@@ -80,13 +80,19 @@ static int testStream(const char *libpath) {
8080 KunStreamContextHandle ctx = kunCreateStream (exec, modu, num_stocks);
8181 CHECK (ctx);
8282
83+ // now dump the stream states
84+ // use null buffer and zero size to get the real buffer size
8385 size_t buf_size = 0 ;
8486 auto status = kunStreamSerializeStates (ctx, KUN_INIT_MEMORY, nullptr , &buf_size);
8587 CHECK (status == KUN_INIT_ERROR);
88+ // second try to allocate buffer and get the real data
89+ size_t buf_size2 = buf_size;
8690 auto states_buffer = new char [buf_size];
8791 status = kunStreamSerializeStates (ctx, KUN_INIT_MEMORY, states_buffer, &buf_size);
8892 CHECK (status == KUN_SUCCESS);
93+ CHECK (buf_size == buf_size2);
8994
95+ // run the stream
9096 size_t handleClose = kunQueryBufferHandle (ctx, " close" );
9197 size_t handleOpen = kunQueryBufferHandle (ctx, " open" );
9298 size_t handleHigh = kunQueryBufferHandle (ctx, " high" );
@@ -96,27 +102,32 @@ static int testStream(const char *libpath) {
96102 size_t handleAlpha101 = kunQueryBufferHandle (ctx, " alpha101" );
97103 // don't need to query the handles everytime when calling kunStreamPushData
98104
99- kunStreamPushData (ctx, handleClose, dataclose);
100- kunStreamPushData (ctx, handleOpen, dataopen);
101- kunStreamPushData (ctx, handleHigh, datahigh);
102- kunStreamPushData (ctx, handleLow, datalow);
103- kunStreamPushData (ctx, handleVol, datavol);
104- kunStreamPushData (ctx, handleAmount, dataamount);
105-
106- kunStreamRun (ctx);
107- memcpy (alpha101, kunStreamGetCurrentBuffer (ctx, handleAlpha101),
108- sizeof (float ) * num_stocks);
109-
110- for (size_t i = 0 ; i < num_stocks; i++) {
111- float expected =
112- (dataclose[i] - dataopen[i]) / (datahigh[i] - datalow[i] + 0.001 );
113- if (std::abs (alpha101[i] - expected) > 1e-5 ) {
114- printf (" Output error at %zu => %f, %f\n " , i, alpha101[i], expected);
115- return 4 ;
105+ auto run_and_check = [&]() {
106+ kunStreamPushData (ctx, handleClose, dataclose);
107+ kunStreamPushData (ctx, handleOpen, dataopen);
108+ kunStreamPushData (ctx, handleHigh, datahigh);
109+ kunStreamPushData (ctx, handleLow, datalow);
110+ kunStreamPushData (ctx, handleVol, datavol);
111+ kunStreamPushData (ctx, handleAmount, dataamount);
112+
113+ kunStreamRun (ctx);
114+ memcpy (alpha101, kunStreamGetCurrentBuffer (ctx, handleAlpha101),
115+ sizeof (float ) * num_stocks);
116+
117+ for (size_t i = 0 ; i < num_stocks; i++) {
118+ float expected =
119+ (dataclose[i] - dataopen[i]) / (datahigh[i] - datalow[i] + 0.001 );
120+ if (std::abs (alpha101[i] - expected) > 1e-5 ) {
121+ printf (" Output error at %zu => %f, %f\n " , i, alpha101[i], expected);
122+ exit (4 );
123+ }
116124 }
117- }
118-
125+ };
126+ run_and_check ();
119127 kunDestoryStream (ctx);
128+
129+ // check restore stream from states
130+ // create a new stream context from the states
120131 ctx = nullptr ;
121132 KunStreamExtraArgs extra_args;
122133 extra_args.version = KUN_API_VERSION;
@@ -126,6 +137,8 @@ static int testStream(const char *libpath) {
126137 status = kunCreateStreamEx (exec, modu, num_stocks, &extra_args, &ctx);
127138 CHECK (status == KUN_SUCCESS);
128139 CHECK (ctx);
140+ // run again to check if the states are restored correctly
141+ run_and_check ();
129142
130143
131144 delete[] dataclose;
0 commit comments