1// tlimits.cpp -- Regression test program, limit tests
2// $Id: tlimits.cpp 1230 2007-03-09 15:58:53Z jcw $
3// This is part of Metakit, the homepage is http://www.equi4.com/metakit.html
4
5#include "regress.h"
6
7void TestLimits() {
8  B(l00, Lots of properties, 0)W(l00a);
9   {
10    c4_String desc;
11
12    for (int i = 1; i < 150; ++i) {
13      char buf[20];
14      sprintf(buf, ",p%d:I", i);
15
16      desc += buf;
17    }
18
19    desc = "a[" + desc.Mid(1) + "]";
20
21    c4_Storage s1("l00a", 1);
22    s1.SetStructure(desc);
23    c4_View v1 = s1.View("a");
24    c4_IntProp p123("p123");
25    v1.Add(p123[123]);
26    s1.Commit();
27
28  }
29  D(l00a);
30  R(l00a);
31  E;
32
33  B(l01, Over 32 Kb of integers, 0)W(l01a);
34   {
35    c4_Storage s1("l01a", 1);
36    s1.SetStructure("a[p1:I]");
37    c4_View v1 = s1.View("a");
38    c4_IntProp p1("p1");
39    v1.SetSize(9000);
40
41    for (int i = 0; i < v1.GetSize(); ++i) {
42      p1(v1[i]) = 1000000L + i;
43
44      A(p1(v1[i]) - 1000000L == i);
45    }
46
47    for (int j = 0; j < v1.GetSize(); ++j) {
48      A(p1(v1[j]) - 1000000L == j);
49    }
50
51    s1.Commit();
52
53    for (int k = 0; k < v1.GetSize(); ++k) {
54      A(p1(v1[k]) - 1000000L == k);
55    }
56
57  }
58  D(l01a);
59  R(l01a);
60  E;
61
62  B(l02, Over 64 Kb of strings, 0)W(l02a);
63   {
64    static char *texts[3] =  {
65      "Alice in Wonderland", "The wizard of Oz", "I'm singin' in the rain"
66    };
67
68    c4_Storage s1("l02a", 1);
69    s1.SetStructure("a[p1:S]");
70    c4_View v1 = s1.View("a");
71    c4_StringProp p1("p1");
72    c4_Row r1;
73
74    for (int i = 0; i < 3500; ++i) {
75      p1(r1) = texts[i % 3];
76      v1.Add(r1);
77
78      A(p1(v1[i]) == (c4_String)texts[i % 3]);
79    }
80
81    for (int j = 0; j < v1.GetSize(); ++j) {
82      A(p1(v1[j]) == (c4_String)texts[j % 3]);
83    }
84
85    s1.Commit();
86
87    for (int k = 0; k < v1.GetSize(); ++k) {
88      A(p1(v1[k]) == (c4_String)texts[k % 3]);
89    }
90
91  }
92  D(l02a);
93  R(l02a);
94  E;
95
96  B(l03, Force sections in storage, 0)W(l03a);
97  W(l03b);
98   {
99    c4_ViewProp p1("p1");
100    c4_IntProp p2("p2");
101
102     {
103      c4_Storage s1("l03a", 1);
104      s1.SetStructure("a[p1[p2:I]]");
105      c4_View v1 = s1.View("a");
106
107      c4_View v2;
108      v2.SetSize(1);
109
110      for (int i = 0; i < 500; ++i) {
111        p2(v2[0]) = 9000+i;
112        v1.Add(p1[v2]);
113      }
114
115      s1.Commit();
116    }
117     {
118      c4_Storage s1("l03a", 0);
119      c4_View v1 = s1.View("a");
120
121      for (int i = 0; i < 500; ++i) {
122        c4_View v2 = p1(v1[i]);
123        A(p2(v2[0]) == 9000+i);
124      }
125
126      c4_FileStream fs1(fopen("l03b", "wb"), true);
127      s1.SaveTo(fs1);
128    }
129     {
130      c4_Storage s1;
131
132      c4_FileStream fs1(fopen("l03b", "rb"), true);
133      s1.LoadFrom(fs1);
134
135      c4_View v1 = s1.View("a");
136
137      for (int i = 0; i < 500; ++i) {
138        c4_View v2 = p1(v1[i]);
139        A(p2(v2[0]) == 9000+i);
140      }
141    }
142  }
143  D(l03a);
144  D(l03b);
145  R(l03a);
146  R(l03b);
147  E;
148
149  B(l04, Modify sections in storage, 0)W(l04a);
150   {
151    c4_ViewProp p1("p1");
152    c4_IntProp p2("p2");
153
154     {
155      c4_Storage s1("l04a", 1);
156      s1.SetStructure("a[p1[p2:I]]");
157      c4_View v1 = s1.View("a");
158
159      c4_View v2;
160      v2.SetSize(1);
161
162      for (int i = 0; i < 500; ++i) {
163        p2(v2[0]) = 9000+i;
164        v1.Add(p1[v2]);
165      }
166
167      s1.Commit();
168    }
169     {
170      c4_Storage s1("l04a", 1);
171      c4_View v1 = s1.View("a");
172      c4_View v2 = p1(v1[0]);
173
174      p2(v2[0]) = 1;
175      // this corrupted file in 1.5: free space was bad after load
176      s1.Commit();
177    }
178     {
179      c4_Storage s1("l04a", 0);
180    }
181  }
182  D(l04a);
183  R(l04a);
184  E;
185
186  B(l05, Delete from 32 Kb of strings, 0)W(l05a);
187   {
188    static char *texts[3] =  {
189      "Alice in Wonderland", "The wizard of Oz", "I'm singin' in the rain"
190    };
191
192    c4_Storage s1("l05a", 1);
193    s1.SetStructure("a[p1:I,p2:S,p3:S]");
194    c4_View v1 = s1.View("a");
195    c4_IntProp p1("p1");
196    c4_StringProp p2("p2"), p3("p3");
197    c4_Row r1;
198
199    for (int i = 0; i < 1750; ++i) {
200      p1(r1) = i;
201      p2(r1) = texts[i % 3];
202      p3(r1) = texts[i % 3];
203      v1.Add(r1);
204
205      A(p2(v1[i]) == (c4_String)texts[i % 3]);
206    }
207
208    for (int j = 0; j < v1.GetSize(); ++j) {
209      A(p1(v1[j]) == j);
210      A(p2(v1[j]) == (c4_String)texts[j % 3]);
211      A(p3(v1[j]) == (c4_String)texts[j % 3]);
212    }
213
214    s1.Commit();
215
216    while (v1.GetSize() > 1)
217    // randomly remove entries
218      v1.RemoveAt((unsigned short)(211 *v1.GetSize()) % v1.GetSize());
219
220    s1.Commit();
221
222  }
223  D(l05a);
224  R(l05a);
225  E;
226
227  B(l06, Bit field manipulations, 0)W(l06a);
228   {
229    c4_IntProp p1("p1");
230    c4_View v2;
231
232     {
233      c4_Storage s1("l06a", 1);
234      s1.SetStructure("a[p1:I]");
235      c4_View v1 = s1.View("a");
236      c4_Row r1;
237
238      for (int i = 2; i <= 256; i <<= 1) {
239        for (int j = 0; j < 18; ++j) {
240          p1(r1) = j &(i - 1);
241
242          v1.InsertAt(j, r1, j + 1);
243          v2.InsertAt(j, r1, j + 1);
244        }
245
246        s1.Commit();
247      }
248    }
249     {
250      c4_Storage s1("l06a", 0);
251      c4_View v1 = s1.View("a");
252
253      int n = v2.GetSize();
254      A(n == v1.GetSize());
255
256      for (int i = 0; i < n; ++i) {
257        long v = p1(v2[i]);
258        A(p1(v1[i]) == v);
259      }
260    }
261
262  }
263  D(l06a);
264  R(l06a);
265  E;
266
267  B(l07, Huge description, 0)W(l07a);
268   {
269    c4_String desc;
270
271    for (int i = 1; i < 150; ++i) {
272      char buf[50];
273      // 1999-07-25: longer size to force over 4 Kb of description
274      sprintf(buf, ",a123456789a123456789a123456789p%d:I", i);
275
276      desc += buf;
277    }
278
279    desc = "a[" + desc.Mid(1) + "]";
280
281    c4_Storage s1("l07a", 1);
282    s1.SetStructure(desc);
283    c4_View v1 = s1.View("a");
284    c4_IntProp p123("p123");
285    v1.Add(p123[123]);
286    s1.Commit();
287
288  }
289  D(l07a);
290  R(l07a);
291  E;
292}
293