fixes to network and client ids; it should work with more than two peers now ;-)
[wootedit.git] / wootext_test.d
blobe92d9e38d011577e1d8af9912dbb517b86dd5eda
1 // coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 // Understanding is not required. Only obedience.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 module woot_test is aliced;
23 import woot2;
25 import iv.prng.pcg32;
26 import iv.vfs.io;
29 // ////////////////////////////////////////////////////////////////////////// //
30 __gshared PCG32 prng;
33 int randomInt (int max) {
34 if (max < 1) return 0;
35 auto res = cast(int)(prng.front%max);
36 prng.popFront();
37 return res;
41 char randomChar () => cast(char)(97+randomInt(26));
44 string randomString (int maxLen=0) {
45 maxLen |= 16;
46 string s;
47 int sLen = randomInt(maxLen+1);
48 foreach (immutable _; 0..sLen) s ~= randomChar();
49 return s;
53 WStrOp[] randomOperation (WString ws) {
54 auto str = ws.toString();
55 // insert?
56 if (str.length == 0 || randomInt(2) == 0) return ws.genInsOp(randomInt(cast(int)str.length), randomString());
57 // delete
58 int len = randomInt(cast(int)str.length)+1;
59 int ofs = (len == str.length ? 0 : randomInt(cast(int)str.length-len));
60 return ws.genDelOp(ofs, len);
64 void applyInRandomOrder (WString ws, WStrOp[] ops) {
65 while (ops.length > 0) {
66 auto pos = randomInt(cast(int)ops.length);
67 ws.applyRemote(ops[pos]);
68 if (ops.length == 1) break;
69 ops[pos] = ops[$-1];
70 ops.length -= 1;
75 // ////////////////////////////////////////////////////////////////////////// //
76 void testAppend () {
77 auto o = new WString(1);
78 o.genInsOp(0, "lorem");
79 o.genInsOp(5, " ipsum");
80 assert(o.toString == "lorem ipsum");
81 //assert(o.ops.length == 3);
82 //writeln("o=", o.toString);
84 auto reso = new TextOperation();
85 reso.ops ~= TextOp("lorem ipsum");
86 reso.ops ~= TextOp(8);
87 reso.ops ~= TextOp(-8);
88 //writeln("x=", reso.toString);
89 //assert(o == TextOperation({"lorem ipsum", 8, -8}))
90 assert(o == reso);
95 void testApply () {
96 string doc = "Lorem ipsum";
97 auto op = new WString(1);
98 op.genInsOp(0, doc);
99 op.genDelOp(0);
100 op.genInsOp(0, 'l');
101 op.genDelOp(5, 4);
102 op.genInsOp(7, "s");
103 //writeln("op=<", op.toString, ">");
104 //writeln("op=", op.chars2str);
105 assert(op.toString == "loressum");
109 void testRandomOps () {
110 auto s0 = new WString(1);
111 auto op0 = s0.genInsOp(0, randomString(42));
112 auto s1 = new WString(2);
113 //writeln(op0.length);
114 //writeln(op0);
115 s1.applyRemote(op0);
116 //writeln(s0.toString);
117 //writeln(s1.toString);
118 //assert(s0.toString == s1.toString);
119 //writeln(s0.toString);
120 WStrOp[] opcoll;
121 foreach (immutable _; 0..42) {
122 auto rop = randomOperation(s0);
123 //writeln(_, ": rop=", rop, "; after=", s0);
124 opcoll ~= rop;
126 applyInRandomOrder(s1, opcoll);
127 assert(s0.toString == s1.toString);
128 //writeln("s0=", s0.mChars);
129 //writeln("s1=", s1.mChars);
130 //assert(s0.mChars.length == s1.mChars.length);
131 //writeln(s0.mChars[0], " -- ", s1.mChars[0]);
133 writeln(s0.mChars[0].id, " -- ", s1.mChars[0].id);
134 writeln(s0.mChars[0].id.mSiteNumber, ", ", s0.mChars[0].id.mOpNumber);
135 writeln(s1.mChars[0].id.mSiteNumber, ", ", s1.mChars[0].id.mOpNumber);
136 writeln(s0.mChars[0].id.mSiteNumber == s1.mChars[0].id.mSiteNumber, " : ", s0.mChars[0].id.mOpNumber == s1.mChars[0].id.mOpNumber);
137 writeln(s0.mChars[0].id == s1.mChars[0].id);
138 assert(s0.mChars[0].id == s1.mChars[0].id);
140 //assert(s0.mChars[0] == s1.mChars[0]);
141 //writeln(s0.mChars);
142 //writeln(s1.mChars);
144 assert(s0.mChars.length == s1.mChars.length);
145 assert(s0.mChars[0] == s1.mChars[0]);
146 foreach (immutable idx; 0..s0.mChars.length) {
147 if (s0.mChars[idx] != s1.mChars[idx]) {
148 import std.format : format;
149 assert(0, "oops: idx=%s; s0c:%s; s1c:%s".format(idx, s0.mChars[idx], s1.mChars[idx]));
153 assert(s0 == s1);
157 void testRandomOps2 () {
158 auto s0 = new WString(1);
159 auto op0 = s0.genInsOp(0, randomString(42));
160 auto s1 = new WString(2);
161 //writeln(op0.length);
162 //writeln(op0);
163 s1.applyRemote(op0);
164 //writeln(s0.toString);
165 //writeln(s1.toString);
166 //assert(s0.toString == s1.toString);
167 //writeln(s0.toString);
168 foreach (immutable _; 0..42) {
169 auto rop = randomOperation(s0);
170 //writeln(_, ": rop=", rop, "; after=", s0);
171 applyInRandomOrder(s1, rop);
172 assert(s0.toString == s1.toString);
177 // ////////////////////////////////////////////////////////////////////////// //
178 void main () {
179 // create first string
180 auto s0 = new WString(1);
181 // create second string
182 auto s1 = new WString(2);
183 // modify first string, fix second
184 s1.applyRemote(s0.genInsOp(0, 'a'));
185 writeln("s0: <", s0.toString, ">; s1: <", s1.toString, ">");
186 // modify first string, fix second
187 s1.applyRemote(s0.genInsOp(0, 'b'));
188 writeln("s0: <", s0.toString, ">; s1: <", s1.toString, ">");
189 // modify first string, fix second
190 s1.applyRemote(s0.genInsOp(1, 'c'));
191 writeln("s0: <", s0.toString, ">; s1: <", s1.toString, ">");
192 auto op0 = s0.genDelOp(0);
193 writeln("op0: ", op0.toString);
194 writeln("s0: <", s0.toString, ">; s1: <", s1.toString, ">");
195 auto op1 = s0.genDelOp(1);
196 writeln("op1: ", op1.toString);
197 writeln("s0: <", s0.toString, ">; s1: <", s1.toString, ">");
198 s1.applyRemote(op1);
199 writeln("s0: <", s0.toString, ">; s1: <", s1.toString, ">");
200 s1.applyRemote(op0);
201 writeln("s0: <", s0.toString, ">; s1: <", s1.toString, ">");
202 s1.applyRemote(op1);
203 writeln("s0: <", s0.toString, ">; s1: <", s1.toString, ">");
204 s1.applyRemote(op0);
205 writeln("s0: <", s0.toString, ">; s1: <", s1.toString, ">");
207 testAppend();
208 testApply();
209 foreach (immutable _; 0..500) testRandomOps();
210 foreach (immutable _; 0..500) testRandomOps2();