1// textend.cpp -- Regression test program, commit extend tests
2// $Id: textend.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
7const int kSize1 = 41;
8const int kSize2 = 85;
9
10void TestExtend() {
11  B(e01, Extend new file, 0)W(e01a);
12   {
13    c4_IntProp p1("p1");
14    c4_Storage s1("e01a", 2);
15    A(s1.Strategy().FileSize() == 0);
16    c4_View v1 = s1.GetAs("a[p1:I]");
17    v1.Add(p1[123]);
18    s1.Commit();
19    A(s1.Strategy().FileSize() == kSize1);
20    v1.Add(p1[456]);
21    s1.Commit();
22    A(s1.Strategy().FileSize() == kSize2);
23  }
24  D(e01a);
25  R(e01a);
26  E;
27
28  B(e02, Extend committing twice, 0)W(e02a);
29   {
30    c4_IntProp p1("p1");
31    c4_Storage s1("e02a", 2);
32    A(s1.Strategy().FileSize() == 0);
33    c4_View v1 = s1.GetAs("a[p1:I]");
34    v1.Add(p1[123]);
35    s1.Commit();
36    A(s1.Strategy().FileSize() == kSize1);
37    s1.Commit();
38    A(s1.Strategy().FileSize() == kSize1);
39    v1.Add(p1[456]);
40    s1.Commit();
41    A(s1.Strategy().FileSize() == kSize2);
42  }
43  D(e02a);
44  R(e02a);
45  E;
46
47  B(e03, Read during extend, 0)W(e03a);
48   {
49    c4_IntProp p1("p1");
50    c4_Storage s1("e03a", 2);
51    A(s1.Strategy().FileSize() == 0);
52    c4_View v1 = s1.GetAs("a[p1:I]");
53    v1.Add(p1[123]);
54    s1.Commit();
55    A(s1.Strategy().FileSize() == kSize1);
56
57     {
58      c4_Storage s2("e03a", 0);
59      c4_View v2 = s2.View("a");
60      A(v2.GetSize() == 1);
61      A(p1(v2[0]) == 123);
62    }
63
64    v1.Add(p1[456]);
65    s1.Commit();
66    A(s1.Strategy().FileSize() == kSize2);
67
68     {
69      c4_Storage s3("e03a", 0);
70      c4_View v3 = s3.View("a");
71      A(v3.GetSize() == 2);
72      A(p1(v3[0]) == 123);
73      A(p1(v3[1]) == 456);
74    }
75  }
76  D(e03a);
77  R(e03a);
78  E;
79
80  B(e04, Extend during read, 0)W(e04a);
81   {
82    c4_IntProp p1("p1");
83
84     {
85      c4_Storage s1("e04a", 2);
86      A(s1.Strategy().FileSize() == 0);
87      c4_View v1 = s1.GetAs("a[p1:I]");
88      v1.Add(p1[123]);
89      s1.Commit();
90      A(s1.Strategy().FileSize() == kSize1);
91    }
92
93    c4_Storage s2("e04a", 0);
94    c4_View v2 = s2.View("a");
95    A(v2.GetSize() == 1);
96    A(p1(v2[0]) == 123);
97
98    c4_Storage s3("e04a", 0); { // open, don't load
99
100
101      c4_Storage s4("e04a", 2);
102      A(s4.Strategy().FileSize() == kSize1);
103      c4_View v4 = s4.View("a");
104      v4.Add(p1[123]);
105      s4.Commit();
106      A(s4.Strategy().FileSize() > kSize1); // == kSize2);
107    }
108
109    c4_View v2a = s2.View("a");
110    A(v2a.GetSize() == 1);
111    A(p1(v2a[0]) == 123);
112
113    c4_View v3 = s3.View("a");
114    A(v3.GetSize() == 1);
115    A(p1(v3[0]) == 123);
116
117  }
118  D(e04a);
119  R(e04a);
120  E;
121
122  B(e05, Test memory mapping, 0)W(e05a);
123   {
124    // this is not a test of MK, but of the underlying system code
125
126     {
127      c4_FileStrategy fs;
128      bool f1 = fs.DataOpen("e05a", 1);
129      A(!f1);
130      fs.DataWrite(0, "hi!", 3);
131      A(fs._failure == 0);
132      A(fs.FileSize() == 3);
133      fs.DataCommit(0);
134      A(fs.FileSize() == 3);
135      fs.ResetFileMapping();
136      if (fs._mapStart != 0) {
137        A(fs._dataSize == 3);
138        c4_String s((char*)fs._mapStart, 3);
139        A(s == "hi!");
140      }
141      fs.DataWrite(3, "hello", 5);
142      A(fs._failure == 0);
143      A(fs.FileSize() == 8);
144      fs.DataCommit(0);
145      A(fs.FileSize() == 8);
146      if (fs._mapStart != 0) {
147        A(fs._dataSize == 3);
148        c4_String s((char*)fs._mapStart, 8);
149        A(s == "hi!hello");
150      }
151      fs.DataWrite(100, "wow!", 4);
152      A(fs._failure == 0);
153      A(fs.FileSize() == 104);
154      fs.DataCommit(0);
155      A(fs.FileSize() == 104);
156      fs.ResetFileMapping();
157      if (fs._mapStart != 0) {
158        A(fs._dataSize == 104);
159        c4_String s((char*)fs._mapStart + 100, 4);
160        A(s == "wow!");
161      }
162    }
163
164    // clear the file, so dump doesn't choke on it
165    FILE *fp = fopen("e05a", "w");
166    A(fp != 0);
167    fclose(fp);
168
169  }
170  D(e05a);
171  R(e05a);
172  E;
173
174  B(e06, Rollback during extend, 0)W(e06a);
175   {
176    c4_IntProp p1("p1");
177    c4_Storage s1("e06a", 2);
178    A(s1.Strategy().FileSize() == 0);
179    c4_View v1 = s1.GetAs("a[p1:I]");
180    v1.Add(p1[123]);
181    s1.Commit();
182    A(s1.Strategy().FileSize() == kSize1);
183
184    c4_Storage s2("e06a", 0);
185    c4_View v2 = s2.View("a");
186    A(v2.GetSize() == 1);
187    A(p1(v2[0]) == 123);
188
189    v1.Add(p1[456]);
190    s1.Commit();
191    A(s1.Strategy().FileSize() == kSize2);
192#if 0
193    /* fails on NT + Samba, though it works fine with mmap'ing disabled */
194    s2.Rollback();
195
196    c4_View v2a = s2.View("a");
197    A(v2a.GetSize() == 2);
198    A(p1(v2a[0]) == 123);
199    A(p1(v2a[1]) == 456);
200#else
201    c4_View v2a = s2.View("a");
202    A(v2a.GetSize() == 1);
203    A(p1(v2a[0]) == 123);
204#endif
205  }
206  D(e06a);
207  R(e06a);
208  E;
209}
210