# This is the source of the test file. The test file is produced by:
# gap -T -A -b -r < test_new.src
# remove the lines after STOP_TEST( "test", 10000 ); at the end of the generated file.
LogTo("test_new");
START_TEST( "Test of the 'openmath' package" );
SizeScreen([80]);;
ValidatePackageInfo( Filename( DirectoriesPackageLibrary( "openmath", "" ), "PackageInfo.g" ) );
LoadPackage("openmath");

### Some examples from file openmath/examples

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); OM_GAP_OUTPUT_STR; OM_GAP_ERROR_STR;
a;
OMGetObject(stream); OM_GAP_OUTPUT_STR; OM_GAP_ERROR_STR;
OMGetObject(stream); OM_GAP_OUTPUT_STR; OM_GAP_ERROR_STR; OM_GAP_OUTPUT_STR := "";;
a;
OMGetObject(stream); OM_GAP_OUTPUT_STR; OM_GAP_ERROR_STR;
OMGetObject(stream); OM_GAP_OUTPUT_STR; OM_GAP_ERROR_STR; OM_GAP_OUTPUT_STR := "";; OM_GAP_ERROR_STR := "";;
OMGetObject(stream); OM_GAP_OUTPUT_STR; OM_GAP_ERROR_STR;
OMGetObject(stream); OM_GAP_OUTPUT_STR; OM_GAP_ERROR_STR;
# there are some objects left on stream, but they are character table examples
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);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
OMGetObject(stream);
CloseStream( stream );

## Tests of conversion from GAP to OpenMath

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

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

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


### test with the different kinds of stream

## OutputTextFile, InputTextFile

s := OutputTextFile( Filename( OMDirectoryTemporary, "test" ), false );;
w := OpenMathXMLWriter( s );;
OMPutObject( w, 123 );
OMPutObject( w, false );
OMPutObject( w, "OutputTextFile, InputTextFile" );
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 );
w := OpenMathXMLWriter( s );;
OMPutObject( w, 123 );
OMPutObject( w, false );
OMPutObject( w, "OutputTextString, InputTextString" );
CloseStream( s );

test_string;

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

## OutputTextNone

s := OutputTextNone(  );
w := OpenMathXMLWriter( s );;
OMPutObject( w, 123 );
OMPutObject( w, false );
OMPutObject( w, "OutputTextNone" );
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 a hexadecimal encoded integer

s := InputTextString( "<OMOBJ><OMI>x10ab2937fed2837a028374</OMI></OMOBJ>" );
OMGetObject(s);
CloseStream(s);

# 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

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

# test that the strings are returned in string representation

stream := InputTextString( "<OMOBJ><OMSTR></OMSTR></OMOBJ> <OMOBJ><OMSTR>ciao</OMSTR></OMOBJ>" );;
IsStringRep( OMGetObject( stream ) );
IsStringRep( OMGetObject( stream ) );
CloseStream(stream);

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

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

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

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

h := HasseDiagram(p);

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

### test for the various supported objects

#I  Searching Method for OMPut with 2 arguments:
#I  Total: 18 entries
#I  Method 1: ``OMPut: for Rationals'', value: 65

OMPrint(Rationals);
OMTest(Rationals);

#I  Method 2: ``OMPut: for Integers'', value: 42

OMPrint(Integers);
OMTest(Integers);

#I  Method 3: ``OMPut: for a range'', value: 40

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

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

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

#I  Method 5: ``OMPut: for NonnegativeIntegers'', value: 31

OMPrint(NonnegativeIntegers);
OMTest(NonnegativeIntegers);

#I  Method 6: ``OMPut: for a character table'', value: 23
#I  Method 7: ``OMPut: for a permutation group'', value: 23

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

#I  Method: ``OMPut: for a group''

OMPrint( Group( [[0,1],[-1,0]],[[1,1],[0,1]] ) );
OMTest( Group( [[0,1],[-1,0]],[[1,1],[0,1]] ) );

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

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

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

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

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

OMPrint(infinity);
OMTest(infinity);

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

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

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

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

#I  Method 13: ``OMPut: for a Hasse diagram'', value: 13

OMPrint(h);

#I  Method 14: ``OMPut: for a finite set'', value: 11

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

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

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

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

OMPrint("OMPut: for a string");
OMTest("OMPut: for a string");
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 17: ``OMPut: for a finite list or collection'', value: 6

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

#I  Method 18: ``OMPut: for a boolean'', value: 3

OMPrint(false);
OMTest(false);

#I  Method 19: ``OMPut: for a float'', value: 3

floats := List( [ 0, "-0", 1, 1/2, -1, "inf", "-inf" ], Float );
OMPrint(floats);
OMTest(floats);
floats := List( [ 11^7, 11^-7, -11^7, -11^-7, "nan" ], Float );
OMPrint(floats);

STOP_TEST( "test", 10000 );
LogTo();
quit;
