summaryrefslogtreecommitdiffstats
path: root/akregator/src/mk4storage/metakit/tests/textend.cpp
blob: 98331f1af74978cd996d8527ce5f9841f369c0f2 (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
// textend.cpp -- Regression test program, commit extend tests
// $Id$
// This is part of Metakit, the homepage is http://www.equi4.com/metakit/

#include "regress.h"

const int kSize1 = 41;
const int kSize2 = 85;

void TestExtend()
{
  B(e01, Extend new file, 0) W(e01a);
  {
    c4_IntProp p1 ("p1");
    c4_Storage s1 ("e01a", 2);
      A(s1.Strategy().FileSize() == 0);
    c4_View v1 = s1.GetAs("a[p1:I]");
    v1.Add(p1 [123]);
    s1.Commit();
      A(s1.Strategy().FileSize() == kSize1);
    v1.Add(p1 [456]);
    s1.Commit();
      A(s1.Strategy().FileSize() == kSize2);
  } D(e01a); R(e01a); E;

  B(e02, Extend committing twice, 0) W(e02a);
  {
    c4_IntProp p1 ("p1");
    c4_Storage s1 ("e02a", 2);
      A(s1.Strategy().FileSize() == 0);
    c4_View v1 = s1.GetAs("a[p1:I]");
    v1.Add(p1 [123]);
    s1.Commit();
      A(s1.Strategy().FileSize() == kSize1);
    s1.Commit();
      A(s1.Strategy().FileSize() == kSize1);
    v1.Add(p1 [456]);
    s1.Commit();
      A(s1.Strategy().FileSize() == kSize2);
  } D(e02a); R(e02a); E;

  B(e03, Read during extend, 0) W(e03a);
  {
    c4_IntProp p1 ("p1");
    c4_Storage s1 ("e03a", 2);
      A(s1.Strategy().FileSize() == 0);
    c4_View v1 = s1.GetAs("a[p1:I]");
    v1.Add(p1 [123]);
    s1.Commit();
      A(s1.Strategy().FileSize() == kSize1);

    {   
      c4_Storage s2 ("e03a", 0);
      c4_View v2 = s2.View("a");
        A(v2.GetSize() == 1);
        A(p1 (v2[0]) == 123);
    }
    
    v1.Add(p1 [456]);
    s1.Commit();
      A(s1.Strategy().FileSize() == kSize2);
    
    {   
      c4_Storage s3 ("e03a", 0);
      c4_View v3 = s3.View("a");
        A(v3.GetSize() == 2);
        A(p1 (v3[0]) == 123);
        A(p1 (v3[1]) == 456);
    }
  } D(e03a); R(e03a); E;

  B(e04, Extend during read, 0) W(e04a);
  {
    c4_IntProp p1 ("p1");
    
    {
      c4_Storage s1 ("e04a", 2);
        A(s1.Strategy().FileSize() == 0);
      c4_View v1 = s1.GetAs("a[p1:I]");
      v1.Add(p1 [123]);
      s1.Commit();
        A(s1.Strategy().FileSize() == kSize1);
    }

    c4_Storage s2 ("e04a", 0);
    c4_View v2 = s2.View("a");
      A(v2.GetSize() == 1);
      A(p1 (v2[0]) == 123);
    
    c4_Storage s3 ("e04a", 0); // open, don't load

    {
      c4_Storage s4 ("e04a", 2);
        A(s4.Strategy().FileSize() == kSize1);
      c4_View v4 = s4.View("a");
      v4.Add(p1 [123]);
      s4.Commit();
        A(s4.Strategy().FileSize() > kSize1); // == kSize2);
    }

    c4_View v2a = s2.View("a");
      A(v2a.GetSize() == 1);
      A(p1 (v2a[0]) == 123);

    c4_View v3 = s3.View("a");
      A(v3.GetSize() == 1);
      A(p1 (v3[0]) == 123);

  } D(e04a); R(e04a); E;

  B(e05, Test memory mapping, 0) W(e05a);
  {
    // this is not a test of MK, but of the underlying system code

    {
      c4_FileStrategy fs;
      bool f1 = fs.DataOpen("e05a", 1);
        A(!f1);
      fs.DataWrite(0, "hi!", 3);
        A(fs._failure == 0);
        A(fs.FileSize() == 3);
      fs.DataCommit(0);
        A(fs.FileSize() == 3);
      fs.ResetFileMapping();
      if (fs._mapStart != 0)
      {
        A(fs._dataSize == 3);
        c4_String s ((char*) fs._mapStart, 3);
        A(s == "hi!");
      }
      fs.DataWrite(3, "hello", 5);
        A(fs._failure == 0);
        A(fs.FileSize() == 8);
      fs.DataCommit(0);
        A(fs.FileSize() == 8);
      if (fs._mapStart != 0)
      {
        A(fs._dataSize == 3);
        c4_String s ((char*) fs._mapStart, 8);
        A(s == "hi!hello");
      }
      fs.DataWrite(100, "wow!", 4);
        A(fs._failure == 0);
        A(fs.FileSize() == 104);
      fs.DataCommit(0);
        A(fs.FileSize() == 104);
      fs.ResetFileMapping();
      if (fs._mapStart != 0)
      {
        A(fs._dataSize == 104);
        c4_String s ((char*) fs._mapStart + 100, 4);
        A(s == "wow!");
      }
    }

      // clear the file, so dump doesn't choke on it
    FILE* fp = fopen("e05a", "w");
      A(fp != 0);
    fclose(fp);

  } D(e05a); R(e05a); E;

  B(e06, Rollback during extend, 0) W(e06a);
  {
    c4_IntProp p1 ("p1");
    c4_Storage s1 ("e06a", 2);
      A(s1.Strategy().FileSize() == 0);
    c4_View v1 = s1.GetAs("a[p1:I]");
    v1.Add(p1 [123]);
    s1.Commit();
      A(s1.Strategy().FileSize() == kSize1);

    c4_Storage s2 ("e06a", 0);
    c4_View v2 = s2.View("a");
      A(v2.GetSize() == 1);
      A(p1 (v2[0]) == 123);
    
    v1.Add(p1 [456]);
    s1.Commit();
      A(s1.Strategy().FileSize() == kSize2);
#if 0
/* fails on NT + Samba, though it works fine with mmap'ing disabled */
    s2.Rollback();

    c4_View v2a = s2.View("a");
      A(v2a.GetSize() == 2);
      A(p1 (v2a[0]) == 123);
      A(p1 (v2a[1]) == 456);
#else
    c4_View v2a = s2.View("a");
      A(v2a.GetSize() == 1);
      A(p1 (v2a[0]) == 123);
#endif
  } D(e06a); R(e06a); E;
}