1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/*
* Modification History
*
* 2001-February-11 Jason Rohrer
* Created.
*
* 2001-February-12 Jason Rohrer
* Added code to test serialization.
*
* 2001-February-24 Jason Rohrer
* Fixed incorrect delete usage.
*
* 2001-April-12 Jason Rohrer
* Changed to comply with new FileInput/OutputStream interface
* (underlying File must be destroyed explicitly).
*/
#include "Expression.h"
#include "ConstantExpression.h"
#include "PowerExpression.h"
#include "ProductExpression.h"
#include "NegateExpression.h"
#include "InvertExpression.h"
#include "SinExpression.h"
#include "LnExpression.h"
#include "SumExpression.h"
#include "ExpressionSerializer.h"
#include "minorGems/io/file/File.h"
#include "minorGems/io/file/FileInputStream.h"
#include "minorGems/io/file/FileOutputStream.h"
// test function for expressions
int main() {
ProductExpression *expression =
new ProductExpression(
new PowerExpression(
new ConstantExpression( 5 ),
new ConstantExpression( 6 ) ),
new NegateExpression(
new SinExpression(
new ConstantExpression( 19 ) ) ) );
InvertExpression *invExpression = new InvertExpression( expression );
SumExpression *sumExpression = new SumExpression(
invExpression, new ConstantExpression( 2 ) );
sumExpression->print();
printf( "\n" );
printf( "%f\n", sumExpression->evaluate() );
printf( "Writing to file.\n" );
char **pathSteps = new char*[2];
pathSteps[0] = new char[10];
pathSteps[1] = new char[10];
sprintf( pathSteps[0], "test" );
sprintf( pathSteps[1], "file" );
int *stepLength = new int[2];
stepLength[0] = 4;
stepLength[1] = 4;
Path *path = new Path( pathSteps, 2, stepLength, false );
File *file = new File( path, "test.out", 8 );
FileOutputStream *outStream = new FileOutputStream( file, false );
char *error = outStream->getLastError();
if( error != NULL ) {
printf( "Error: %s\n", error );
delete error;
}
ExpressionSerializer::serializeExpression( sumExpression, outStream );
delete outStream;
delete file;
printf( "Reading back in from file.\n" );
pathSteps = new char*[2];
pathSteps[0] = new char[10];
pathSteps[1] = new char[10];
sprintf( pathSteps[0], "test" );
sprintf( pathSteps[1], "file" );
stepLength = new int[2];
stepLength[0] = 4;
stepLength[1] = 4;
path = new Path( pathSteps, 2, stepLength, false );
file = new File( path, "test.out", 8 );
FileInputStream *inStream = new FileInputStream( file );
error = inStream->getLastError();
if( error != NULL ) {
printf( "Error: %s\n", error );
delete error;
}
Expression *readExpression;
ExpressionSerializer::deserializeExpression( &readExpression,
inStream );
delete inStream;
delete file;
readExpression->print();
printf( "\n" );
printf( "%f\n", readExpression->evaluate() );
delete sumExpression;
delete readExpression;
return 0;
}
|