summaryrefslogtreecommitdiffstats
path: root/akregator/src/mk4storage/metakit/tests/tstore5.cpp
blob: 1e50b91f3df6c6d84eb8d8315a13a78efa0fb451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// tstore5.cpp -- Regression test program, storage tests, part 5
// $Id$
// This is part of Metakit, see http://www.equi4.com/metakit/

#include "regress.h"

void TestStores5()
{
  B(s40, LoadFrom after commit, 0) W(s40a);
  {
    c4_IntProp p1 ("p1");

    { // create datafile by streaming out
      c4_Storage s1;
      s1.SetStructure("a[p1:I]");

      c4_View v1 = s1.View("a");
      v1.Add(p1 [123]);
	A(p1 (v1[0]) == 123);
	A(v1.GetSize() == 1);

      c4_FileStream fs1 (fopen("s40a", "wb"), true);
      s1.SaveTo(fs1);
    }
    { // it should load just fine
      c4_Storage s2;
      c4_FileStream fs1 (fopen("s40a", "rb"), true);
      bool ok = s2.LoadFrom(fs1);
      	A(ok);

      c4_View v1 = s2.View("a");
	A(p1 (v1[0]) == 123);
	A(v1.GetSize() == 1);
    }
    { // open the datafile and commit a change
      c4_Storage s3 ("s40a", true);

      c4_View v1 = s3.View("a");
	A(p1 (v1[0]) == 123);
	A(v1.GetSize() == 1);
      p1 (v1[0]) = 456;
      s3.Commit();
	A(p1 (v1[0]) == 456);
	A(v1.GetSize() == 1);
    }
    { // it should load fine and show the last changes
      c4_Storage s4;
      c4_FileStream fs1 (fopen("s40a", "rb"), true);
      bool ok = s4.LoadFrom(fs1);
      	A(ok);

      c4_View v1 = s4.View("a");
	A(p1 (v1[0]) == 456);
	A(v1.GetSize() == 1);
    }
    { // it should open just fine in the normal way as well
      c4_Storage s5 ("s40a", false);
      c4_View v1 = s5.View("a");
	A(p1 (v1[0]) == 456);
	A(v1.GetSize() == 1);
    }
  } D(s40a); R(s40a); E;

    // 2002-03-13: failure on Win32, Modify calls base class GetNthMemoCol
  B(s41, Partial modify blocked, 0) W(s41a);
  {
    c4_BytesProp p1 ("p1");
    c4_Storage s1 ("s41a", true);
    c4_View v1 = s1.GetAs("a[_B[p1:B]]");

    // custom viewers did not support partial access in 2.4.3
    c4_View v2 = v1.Blocked();
    s1.Commit();

    v2.SetSize(1);

    c4_BytesRef m = p1 (v2[0]);
    m.Modify(c4_Bytes ("abcdefgh", 8), 0);

    s1.Commit();

  } D(s41a); R(s41a); E;

  B(s42, Get descriptions, 0)
  {
    c4_Storage s1;
    s1.SetStructure("a[p1:I],b[p2:S]");
    
      c4_String x1 = s1.Description();
      A(x1 == "a[p1:I],b[p2:S]");
    
      c4_String x2 = s1.Description("b");
      A(x2 == "p2:S");
    
      const char* cp = s1.Description("c");
      A(cp == 0);
  } E;

    // 2002-04-24: VPI subview ints clobbered
  B(s43, View reuse after sub-byte ints, 0) W(s43a);
  {
    c4_IntProp p1 ("p1");
    c4_Storage s1 ("s43a", true);
    c4_View v1 = s1.GetAs("a[p1:I]");

    v1.Add(p1 [0]);
    v1.Add(p1 [1]);
    s1.Commit();

    v1.SetSize(1); // 1 is an even trickier bug than 0
    s1.Commit();

    // adding the following two lines works around the 2.4.4 bug
    //s1.Rollback();
    //v1 = s1.GetAs("a[p1:I]");
    
    v1.Add(p1 [12345]);
    s1.Commit();
    
      //int n = p1 (v1[1]);
      A(p1 (v1[1]) == 12345);

  } D(s43a); R(s43a); E;

  B(s44, Bad memo free space, 0) W(s44a);
  {
    c4_IntProp p1 ("p1");
    c4_BytesProp p2 ("p2");
    c4_Storage s1 ("s44a", true);
    c4_View v1 = s1.GetAs("a[p1:I,p2:B]");

    c4_Bytes data;
    t4_byte* p = data.SetBuffer(12345);
    for (int i = 0; i < data.Size(); ++i)
      p[i] = (t4_byte) i;

    v1.Add(p2 [data]);
    s1.Commit();

    p1 (v1[0]) = 1;
    s1.Commit();

    p1 (v1[0]) = 0;
    s1.Commit();

    c4_Bytes temp = p2 (v1[0]);
      A(temp == data); // this failed in 2.4.5

  } D(s44a); R(s44a); E;

  B(s45, Bad subview memo free space, 0) W(s45a);
  {
    c4_IntProp p1 ("p1");
    c4_ViewProp p2 ("p2");
    c4_BytesProp p3 ("p3");
    c4_Storage s1 ("s45a", true);
    c4_View v1 = s1.GetAs("a[p1:I,p2[p3:B]]");

    c4_Bytes data;
    t4_byte* p = data.SetBuffer(12345);
    for (int i = 0; i < data.Size(); ++i)
      p[i] = (t4_byte) i;

    v1.SetSize(1);
    c4_View v2 = p2 (v1[0]);
    v2.Add(p3 [data]);
    s1.Commit();

    p1 (v1[0]) = 1;
    s1.Commit();

    p1 (v1[0]) = 0;
    s1.Commit();

    c4_View v3 = p2 (v1[0]);
    c4_Bytes temp = p3 (v3[0]);
      A(temp == data); // this failed in 2.4.5

  } D(s45a); R(s45a); E;

  B(s46, LoadFrom after commit, 0) W(s46a);
  {
    c4_IntProp p1 ("p1");

    {
      c4_Storage s1 ("s46a", true);
      s1.SetStructure("a[p1:I]");
      c4_View v1 = s1.View("a");

      v1.Add(p1 [11]);
      v1.Add(p1 [22]);
      v1.Add(p1 [33]);
      v1.Add(p1 [44]);
      v1.Add(p1 [55]);
      v1.Add(p1 [66]);
      v1.Add(p1 [77]);
      v1.Add(p1 [88]);
      v1.Add(p1 [99]);
      
      s1.Commit();
    }
    {
      c4_Storage s2 ("s46a", true);
      c4_View v2 = s2.View("a");

      v2.Add(p1 [1000]); // force 1->2 byte ints
      v2.InsertAt(7, c4_Row ());
      v2.InsertAt(4, c4_Row ());

      //for (int i = 6; i <= 9; ++i) printf("%d\n", (int) p1 (v2[i]));

      A(p1 (v2[6]) == 66);
      A(p1 (v2[8]) == 0);
      A(p1 (v2[9]) == 88);
      A(p1 (v2[7]) == 77); // this failed in 2.4.6

      s2.Commit();
    }
  } D(s46a); R(s46a); E;

    // 2004-01-16 bad property type crashes MK 2.4.9.2 and before
    // this hits an assertion in debug mode, so then it has to be disabled
  B(s47, Defining bad property type, 0)
  {
    c4_IntProp p1 ("p2");
  
    c4_Storage s1;
#if defined(NDEBUG)
    c4_View v1 = s1.GetAs("v1[p1:A]");
#else
    // assertions are enabled, turn this into a dummy test instead
    c4_View v1 = s1.GetAs("v1[p1:I]");
#endif
    v1.Add(p1 [123]);

      A(v1.GetSize() == 1);
      A(p1 (v1 [0]) == 123);
  } E;

    // 2004-01-18 file damaging bug, when resizing a comitted subview
    // to empty, committing, and then resizing back to containing data.
    // Fortunately this usage pattern never happened in blocked views!
  B(s48, Resize subview to zero and back, 0) W(s48a); W(s48b);
  {
    {
      c4_Storage s1 ("s48a", true);
      c4_View v1 = s1.GetAs("v1[v2[p1:I]]");
      v1.SetSize(1);
      s1.Commit();
    }
    {
      c4_Storage s1 ("s48a", true);
      c4_View v1 = s1.View("v1");
      v1.SetSize(0);
      s1.Commit();
      // the problem is that the in-memory copy has forgotten that it
      // has nothing left on disk, and a comparison is done later on to
      // avoid saving unmodified data - the bad decision is that data has
      // not changed, but actually it has and must be reallocated!
      // (fixes are in c4_FormatV::Insert and c4_FormatV::Remove)
      v1.SetSize(1);
      s1.Commit();
      // at this point, the 2.4.9.2 file is corrupt!
      c4_FileStream fs1 (fopen("s48b", "wb"), true);
      s1.SaveTo(fs1);
    }
    {
      // using this damaged datafile will then crash
      c4_Storage s1 ("s48a", false);
      c4_View v1 = s1.View("v1");
      v1.SetSize(2);
    }
  } D(s48a); D(s48b); R(s48a); R(s48b); E;

    // 2004-01-20 better handling of bad input: ignore repeated props
  B(s49, Specify conflicting properties, 0) W(s49a);
  {
    c4_Storage s1 ("s49a", true);
    c4_View v1 = s1.GetAs("v1[p1:I,p1:S]");
    c4_View v2 = s1.GetAs("v2[p1:I,P1:S]");
    c4_View v3 = s1.GetAs("v3[v3[^]]");
      c4_String x1 = s1.Description();
      A(x1 == "v1[p1:I],v2[p1:I],v3[v3[^]]");
    s1.Commit();
  } D(s49a); E;
}