root/trunk/Serial/test1.d

Revision 592, 2.6 kB (checked in by BCS, 3 years ago)

spelling

Line 
1 module test1;
2
3 import std.stdio;
4 import std.string;
5
6 import Serialize;
7 import StringSourceSink;
8 import Third;
9
10 class CS
11 {
12     mixin SerializableRecurring!();
13     int i = 5;  float j = 3.1415926;
14     this(){}    this(int) { i++; j++; }
15     char[] toString() { return format("<%s,%s>", this.tupleof); }
16 }
17
18 class DS : CS
19 {
20     mixin SerializableRecurring!();
21     real f = 2.717;
22     this(){}    this(int) { f++; super(0); }
23     char[] toString() { return format("<%s,%s>", super.toString()[1..$-1], this.tupleof); }
24 }
25
26 /// show arrays
27
28 struct TS
29 {
30     mixin Serializable!();
31     int ti;
32     char[] toString() { return format("<%s>", this.tupleof); }
33 }
34
35 struct SS
36 {
37     mixin Serializable!();
38     int foo; float bar; CS cs; char[] someString; int[][] data; TS[] tdata;
39     char[] toString() { return format("<%s,%s,%s,%s,%s,%s>", this.tupleof); }
40 }
41
42 /// show aliased references
43
44 struct SC
45 {
46     mixin Serializable!();
47     CS a; CS b;
48     char[] toString() { return format("<%s,%s>", this.tupleof); }
49 }
50
51 /// Show 3rd party types
52
53 struct TPS
54 {
55     int i = 2; int j = 3; char[] str = "five";
56     char[] toString() { return format("<%s,%s,%s>", this.tupleof);}
57 }
58
59 void TPSm(TPS v, SinkHandle h)
60 {
61     Push!("i", int)(v.i, h);    Push!("j", int)(v.j, h);    Push!("str", char[])(v.str, h);
62 }
63 TPS TPSd(SourceHandle h)
64 {
65     TPS ret;
66     ret.i = Pull!("i", int)(h); ret.j = Pull!("j", int)(h); ret.str = Pull!("str", char[])(h);
67     return ret;
68 }
69 mixin WorkWith!(TPS, TPSm, TPSd);
70
71 struct ST
72 {
73     mixin Serializable!();
74     char[] name = "some text to test";  TPS a = TPS(3, 5, "eight");
75     char[] toString() { return format("<%s,%s>", this.tupleof); }
76 }
77
78 void main()
79 {
80     SS ss = SS(42, 2.717, null, "<weee!'\"&>", [[cast(int)6,9,42][],[cast(int)6,9,54]], [TS(3),TS(1)]);
81     SC sc;
82     Si c;
83     ST st;
84
85     ss.cs = null;       c = new Si();
86     ss.Serialize(c);    writef("%s\n\t%s\n", c.get, SS.Deserialize(new So(c.get)));
87    
88     ss.cs = new CS(1);  c = new Si();
89     ss.Serialize(c);    writef("%s\n\t%s\n", c.get, SS.Deserialize(new So(c.get)));
90    
91     ss.cs = new DS(1);  c = new Si();
92     ss.Serialize(c);    writef("%s\n\t%s\n", c.get, SS.Deserialize(new So(c.get)));
93
94     c = new Si();   
95     (new DS(1)).Serialize(c);   writef("%s\n\t%s\n", c.get, CS.Deserialize(new So(c.get)));
96
97     st.name = "somthing to expect"; c = new Si();
98     st.Serialize(c); writef("%s\n\t%s\n", c.get, ST.Deserialize(new So(c.get)));
99
100     sc.a = new CS(1);   sc.b = new CS(1);   c = new Si();   sc.Serialize(c);
101     auto same = SC.Deserialize(new So(c.get));
102     writef("%s\n\t%s\n", c.get, same);
103     assert(same.a !is same.b);
104    
105     sc.b = sc.a;    c = new Si();   sc.Serialize(c);
106     auto dif = SC.Deserialize(new So(c.get));
107     writef("%s\n\t%s\n", c.get, dif);
108     assert(dif.a is dif.b);
109 }
Note: See TracBrowser for help on using the browser.