source: trunk/src/mesh/test_grid.cc @ 349

Last change on this file since 349 was 315, checked in by cxsong, 18 years ago

modified test_grid for multiple objects

File size: 1.8 KB
Line 
1#include <stdio.h>
2#include "serializer.h"
3#include "grid1d.h"
4
5//
6// This example show how to create a grid1d object, put it into
7// a serializer object and ask the serializer for a serialized byte stream.
8//
9
10#define Num_points 20
11
12static double points[20] = {
13        0.261243291268,
14        0.159055762008,
15        0.214550893827,
16        0.11741510008,
17        0.119735699467,
18        0.15196145742,
19        0.0245663797288,
20        0.128903081711,
21        0.0927746958394,
22        0.0465364541799,
23        0.531606236106,
24        0.252448742721,
25        0.348575614391,
26        0.180939456908,
27        0.0251118046222,
28        0.810354715199,
29        0.0980414196039,
30        0.392354903151,
31        0.151346474849,
32        0.368703495654
33};
34
35void writeToFile(char* buf, int len, const char* filename)
36{
37        if (buf != NULL) {
38                FILE* fp = fopen(filename, "w");
39                fwrite(buf, 1, len, fp);
40                fclose(fp);
41        }
42}
43
44int main()
45{
46        int i, j, err, nbytes;
47        const char* name1 = "output.grid(g1d)";
48        const char* name2 = "output.grid(g2d)";
49
50        printf("Testing RpGrid1d\n");
51
52        RpGrid1d* grid1 = new RpGrid1d(name1, 20);
53        grid1->addAllPoints(&(points[0]), 20);
54
55        RpGrid1d* grid2 = new RpGrid1d(name2, 20);
56        grid2->addAllPoints(&(points[0]), 20);
57
58        printf("Testing serializer\n");
59
60        RpSerializer myvis;
61        myvis.addObject(grid1);
62        myvis.addObject(grid2);
63
64        char* buf = myvis.serialize();
65
66        // write the byte stream to a file for verification
67        nbytes = myvis.numBytes();
68        writeToFile(buf, nbytes, "out.g2");
69
70        //myvis.print();
71       
72        printf("Testing serializer::deserializer\n");
73
74        RpSerializer newvis;
75        newvis.deserialize(buf);
76        newvis.print();
77
78        printf("Testing serializer::getObject\n");
79
80        RpGrid1d* ptr1 = (RpGrid1d*) newvis.getObject(name1);
81        if (ptr1 != NULL)
82                ptr1->print();
83        else
84                printf("%s not found\n", name1);
85
86        RpGrid1d* ptr2 = (RpGrid1d*) newvis.getObject(name2);
87        if (ptr2 != NULL)
88                ptr2->print();
89        else
90                printf("%s not found\n", name2);
91
92        delete [] buf;
93
94        return 0;
95}
96
97
Note: See TracBrowser for help on using the repository browser.