# This is the source of the test file. The test file is produced by:
# gap < test_new.src
# remove the line 'gap> LogTo();' at the end of the generated file.

LogTo("test_new");

# TestFile, by Marco Costantini, of the 'openmath' package.

### Start

START_TEST( "Test of the 'openmath' package" );
SizeScreen([80]);;


ValidatePackageInfo( Filename( DirectoriesPackageLibrary( "openmath", "" ), "PackageInfo.g" ) );


LoadPackage("openmath");



### Some examples from file openmath/examples


## Example 1:

stream := InputTextFile(Filename(
        DirectoriesPackageLibrary("openmath","tst"),"test3.omt"));;
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);

CloseStream( stream );


# similar, for file tst/test3.bin: binary encoding

stream := InputTextFile(Filename(
        DirectoriesPackageLibrary("openmath","tst"),"test3.bin"));;
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);

CloseStream( stream );


# similar, for file tst/test4.out: gpipe encoding

stream := InputTextFile(Filename(
        DirectoriesPackageLibrary("openmath","tst"),"test4.out"));;
OMgetObjectByteStream(stream);
OMgetObjectByteStream(stream);
CloseStream( stream );


# similar, for file tst/test5.omt

stream := InputTextFile(Filename(DirectoriesPackageLibrary("openmath","tst"),"test5.omt"));;
OMGetObject(stream);
CloseStream( stream );


# similar, for file tst/test6.omt

stream := InputTextFile(Filename(DirectoriesPackageLibrary("openmath","tst"),"test6.omt"));;
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
CloseStream( stream );


# similar, for file tst/test_new.omt

stream := InputTextFile(Filename(DirectoriesPackageLibrary("openmath","tst"),"test_new.omt"));;
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
# there are 3 objects left on stream, but they are from CD "error"
CloseStream( stream );


## Example 2:

g := SymmetricGroup(5);
t := "";
s := OutputTextString(t, true);
OMPutObject(s, g);
CloseStream(s);

s := InputTextString(t);
h := OMGetObject(s);
CloseStream(s);
h = g;


## Example 4:

g := SymmetricGroup(5);
OMPrint(g);



### test with the different kinds of stream


## OutputTextFile, InputTextFile

s := OutputTextFile( Filename( OMDirectoryTemporary, "test" ), false );;
OMPutObject( s, 123 );
OMPutObject( s, false );
OMPutObject( s, "ciao" );
CloseStream( s );

s := InputTextFile( Filename( OMDirectoryTemporary, "test" ) );;
OMGetObject( s );
OMGetObject( s );
OMGetObject( s );
CloseStream( s );

RemoveFile( Filename( OMDirectoryTemporary, "test" ) );


## OutputTextUser,  InputTextUser


# how do one tests OutputTextUser?

# how do one tests InputTextUser?


## OutputTextString, InputTextString

test_string := "";

s := OutputTextString( test_string, false );
OMPutObject( s, 123 );
OMPutObject( s, false );
OMPutObject( s, "ciao" );
CloseStream( s );

test_string;

s := InputTextString( test_string );
OMGetObject( s );
OMGetObject( s );
OMGetObject( s );
CloseStream( s );


## OutputTextNone, InputTextNone

s := OutputTextNone(  );
OMPutObject( s, 123 );
OMPutObject( s, false );
OMPutObject( s, "ciao" );
CloseStream( s );

# how do one tests InputTextNone?


## InputOutputLocalProcess

# stream returns XML encoding

tpipe_exec := Filename( DirectoriesPackageLibrary( "openmath", "OMCv1.3c/src" ), "tpipe" );;
s := InputOutputLocalProcess( OMDirectoryTemporary, tpipe_exec, [ "-ux", "-", "-", ] );

OMPutObject( s, 123 );
OMPutObject( s, false );
OMPutObject( s, "ciao" );

OMGetObject( s );
OMGetObject( s );
OMGetObject( s );
CloseStream( s );

# stream returns binary encoding

tpipe_exec := Filename( DirectoriesPackageLibrary( "openmath", "OMCv1.3c/src" ), "tpipe" );;
s := InputOutputLocalProcess( OMDirectoryTemporary, tpipe_exec, [ "-ub", "-", "-", ] );

OMPutObject( s, 123 );
OMPutObject( s, false );
OMPutObject( s, "ciao" );

OMGetObject( s );
OMGetObject( s );
OMGetObject( s );
CloseStream( s );



### miscellaneous tests

# test with binary encoding and InputTextString

s := InputTextString( "\030\020\b\007\cminmax1max\020\b\004\cset1set\>\>\>\t\>\005\021\021\031" );
OMGetObject(s);
CloseStream(s);

# test with gpipe encoding and InputTextString

gpipe_string := List( [ 24, 16, 8, 109, 105, 110, 109, 97, 120, 49, 255, 109, 97, 120, 255, 16, 8, 115, 
101, 116, 49, 255, 115, 101, 116, 255, 2, 49, 255, 2, 57, 255, 2, 53, 255, 17, 17, 25 ], CHAR_INT );;
s := InputTextString( gpipe_string );
OMgetObjectByteStream(s);
CloseStream(s);


# test of a long object

OMTest(10^4000);


# test of escaped XML chars

s := InputTextString( "<OMOBJ> <OMSTR>&lt;&amp;&gt;&quot;&apos;</OMSTR> </OMOBJ>" );
OMGetObject(s);
CloseStream(s);

# test of a strings with all the chars (but '+' is not supported)

OMTest( List( Union( [4..42], [44..127] ), CHAR_INT ) );



### test for the various supported objects

#I  Searching Method for OMPut with 2 arguments:
#I  Total: 16 entries
#I  Method 1: ``OMPut: for a range'', value: 40

OMTest([3..10]);
OMPrint([3..10]);

#I  Method 2: ``OMPut: for a matrix'', value: 32

OMPrint([[11,12],[21,22]]);
OMTest([[11,12],[21,22]]);

#I  Method 3: ``OMPut: for a character table'', value: 23
#I  Method 4: ``OMPut: for a group'', value: 23

OMPrint(AlternatingGroup(6));
OMTest(AlternatingGroup(6));

#I  Method 5: ``OMPut: for an integer'', value: 20

OMPrint(10^72);
OMTest(10^72);

#I  Method 6: ``OMPut: for a rational'', value: 19

OMPrint(10/72);
OMTest(10/72);

#I  Method 7: ``OMPut: for infinity'', value: 18

OMPrint(infinity);
OMTest(infinity);

#I  Method 8: ``OMPut: for a proper cyclotomic'', value: 18

OMPrint(2/3+5/4*E(4));
OMTest(2/3+5/4*E(4));

#I  Method 9: ``OMPut: for a row vector'', value: 14

OMPrint([1,2,3]);
OMTest([1,2,3]);

#I  Method 10: ``OMPut: for a hasse diagram'', value: 13
#I  Method 11: ``OMPut: for a set'', value: 10

OMPrint([]);
OMTest([]);
OMPrint(Set([true,false]));
OMTest(Set([true,false]));

#I  Method 12: ``OMPut: for a permutation'', value: 10

OMPrint((1,3,5));
OMTest((1,3,5));

#I  Method 13: ``OMPut: for a string'', value: 7

OMPrint("ciao");
OMTest("ciao");
OMPrint(Set(['a','b']));
OMTest(Set(['a','b']));
OMPrint("");
OMTest("");
# also the escaped XML chars
OMPrint("&");
OMTest("&");
OMPrint("<");
OMTest("<");
OMPrint("<&>\"'");
OMTest("<&>\"'");
OMPrint("&lt;&amp;");
OMTest("&lt;&amp;");


#I  Method 14: ``OMPut: for a function'', value: 3
#I  Method 15: ``OMPut: for a boolean'', value: 3

OMPrint(false);
OMTest(false);

#I  Method 16: ``OMPut: for a list or collection'', value: 3

OMPrint([ [ 1 .. 10 ], [  ], false, true, infinity, 1, 0 ]);
OMTest([ [ 1 .. 10 ], [  ], false, true, infinity, 1, 0 ]);



### test of Hasse (there was a bug)

d := Domain(["0","a","b","c","1"]);
Elements(d);
Size(d);

r := BinaryRelationByElements(d,
[ Tuple(["0","a"]),
Tuple(["0","b"]),
Tuple(["0","c"]),
Tuple(["a","1"]),
Tuple(["b","1"]),
Tuple(["c","1"])]);

p := TransitiveClosureBinaryRelation(ReflexiveClosureBinaryRelation(r));
IsPartialOrderBinaryRelation(p);

h := HasseDiagram(p);

# the bug was here.
Elements(UnderlyingRelation(h));



### end of test file

STOP_TEST( "test", 10000 );

LogTo();
quit;

