edits
[iotcloud.git] / version2 / src / C / Test.C
1 #include "Table.h"
2 #include "IoTString.h"
3 #include "TimingSingleton.h"
4 #include "TransactionStatus.h"
5
6 #define NUMBER_OF_TESTS 2
7
8 int main(int numargs, char ** args) {
9         TimingSingleton * timer = TimingSingleton_getInstance();
10
11         bool foundError = false;
12         Vector<TransactionStatus *> * transStatusList = new Vector<TransactionStatus *>();
13
14         // Setup the 2 clients
15         IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
16         IoTString * password = new IoTString("reallysecret");
17         Table * t1 = new Table(baseurl, password, 321, -1);
18         t1->initTable();
19         printf("T1 Ready\n");
20
21         Table * t2 = new Table(baseurl, password, 351, -1);
22         t2->update();
23         printf("T2 Ready\n");
24
25         delete baseurl; delete password;
26         
27         // Make the Keys
28         printf("Setting up keys\n");
29         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
30                 printf("%d\n",i);
31                 char buffer[80];
32                 sprintf(buffer, "a%d", i);
33                 IoTString *ia = new IoTString(buffer);
34                 sprintf(buffer, "b%d", i);
35                 IoTString *ib = new IoTString(buffer);
36                 sprintf(buffer, "c%d", i);
37                 IoTString *ic = new IoTString(buffer);
38                 sprintf(buffer, "d%d", i);
39                 IoTString *id = new IoTString(buffer);
40                 t1->createNewKey(ia, 321);
41                 t1->createNewKey(ib, 351);
42                 t2->createNewKey(ic, 321);
43                 t2->createNewKey(id, 351);
44                 delete ia;
45                 delete ib;
46                 delete ic;
47                 delete id;
48         }
49         
50         // Do Updates for the keys
51         printf("Setting Key-Values...\n");
52         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
53                 printf("%d\n", i);
54                 char buffer[80];
55                 sprintf(buffer, "a%d", i);
56                 IoTString * iKeyA = new IoTString(buffer);
57                 IoTString * iValueA = new IoTString(buffer);
58
59                 sprintf(buffer, "b%d", i);
60                 IoTString * iKeyB = new IoTString(buffer);
61                 IoTString * iValueB = new IoTString(buffer);
62                 
63                 sprintf(buffer, "c%d", i);
64                 IoTString * iKeyC = new IoTString(buffer);
65                 IoTString * iValueC = new IoTString(buffer);
66                 
67                 sprintf(buffer, "d%d", i);
68                 IoTString * iKeyD = new IoTString(buffer);
69                 IoTString * iValueD = new IoTString(buffer);
70
71                 t1->startTransaction();
72                 t1->put(iKeyA, iValueA);
73                 transStatusList->add(t1->commitTransaction());
74                 delete iKeyA; delete iValueA;
75                 
76                 t1->startTransaction();
77                 t1->put(iKeyB, iValueB);
78                 transStatusList->add(t1->commitTransaction());
79                 delete iKeyB; delete iValueB;
80                 
81                 t2->startTransaction();
82                 t2->put(iKeyC, iValueC);
83                 transStatusList->add(t2->commitTransaction());
84                 delete iKeyC; delete iValueC;
85                 
86                 t2->startTransaction();
87                 t2->put(iKeyD, iValueD);
88                 transStatusList->add(t2->commitTransaction());
89                 delete iKeyD; delete iValueD;
90         }
91         
92         printf("Updating Clients...\n");
93         t1->update();
94         t2->update();
95         
96         printf("Checking Key-Values...\n");
97         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
98                 char buffer[80];
99                 sprintf(buffer, "a%d", i);
100                 IoTString * iKeyA = new IoTString(buffer);
101                 IoTString * iValueA = new IoTString(buffer);
102
103                 sprintf(buffer, "b%d", i);
104                 IoTString * iKeyB = new IoTString(buffer);
105                 IoTString * iValueB = new IoTString(buffer);
106                 
107                 sprintf(buffer, "c%d", i);
108                 IoTString * iKeyC = new IoTString(buffer);
109                 IoTString * iValueC = new IoTString(buffer);
110                 
111                 sprintf(buffer, "d%d", i);
112                 IoTString * iKeyD = new IoTString(buffer);
113                 IoTString * iValueD = new IoTString(buffer);
114                 
115                 IoTString *testValA1 = t1->getCommitted(iKeyA);
116                 IoTString *testValB1 = t1->getCommitted(iKeyB);
117                 IoTString *testValC1 = t1->getCommitted(iKeyC);
118                 IoTString *testValD1 = t1->getCommitted(iKeyD);
119                 
120                 IoTString *testValA2 = t2->getCommitted(iKeyA);
121                 IoTString *testValB2 = t2->getCommitted(iKeyB);
122                 IoTString *testValC2 = t2->getCommitted(iKeyC);
123                 IoTString *testValD2 = t2->getCommitted(iKeyD);
124                 
125                 if ((testValA1 == NULL) || (testValA1->equals(iValueA) == false)) {
126                         printf("Key-Value t1 incorrect: keyA\n");
127                         foundError = true;
128                 }
129                 
130                 if ((testValB1 == NULL) || (testValB1->equals(iValueB) == false)) {
131                         printf("Key-Value t1 incorrect: keyB\n");
132                         foundError = true;
133                 }
134
135                 if ((testValC1 == NULL) || (testValC1->equals(iValueC) == false)) {
136                         printf("Key-Value t1 incorrect: keyC\n");
137                         foundError = true;
138                 }
139                 
140                 if ((testValD1 == NULL) || (testValD1->equals(iValueD) == false)) {
141                         printf("Key-Value t1 incorrect: keyD\n");
142                         foundError = true;
143                 }
144                 
145                 if ((testValA2 == NULL) || (testValA2->equals(iValueA) == false)) {
146                         printf("Key-Value t2 incorrect: keyA     testValA2\n");
147                         foundError = true;
148                 }
149                 
150                 if ((testValB2 == NULL) || (testValB2->equals(iValueB) == false)) {
151                         printf("Key-Value t2 incorrect: keyB     testValB2\n");
152                         foundError = true;
153                 }
154                 
155                 if ((testValC2 == NULL) || (testValC2->equals(iValueC) == false)) {
156                         printf("Key-Value t2 incorrect: keyC     testValC2\n");
157                         foundError = true;
158                 }
159                 
160                 if ((testValD2 == NULL) || (testValD2->equals(iValueD) == false)) {
161                         printf("Key-Value t2 incorrect: keyD     testValD2\n");
162                         foundError = true;
163                 }
164                 delete iKeyA; delete iValueA;
165                 delete iKeyB; delete iValueB;
166                 delete iKeyC; delete iValueC;
167                 delete iKeyD; delete iValueD;
168                 delete testValA1; delete testValA2;
169                 delete testValB1; delete testValB2;
170                 delete testValC1; delete testValC2;
171                 delete testValD1; delete testValD2;
172         }
173
174         for (uint i = 0; i < transStatusList->size(); i++) {
175                 TransactionStatus * status = transStatusList->get(i);
176                 if (status->getStatus() != TransactionStatus_StatusCommitted) {
177                         foundError = true;
178                         printf("Status error\n");
179                 }
180                 delete status;
181         }
182         
183         if (foundError) {
184                 printf("Found Errors...\n");
185         } else {
186                 printf("No Errors Found...\n");
187         }
188
189         delete transStatusList;
190         delete t1;
191         delete t2;
192 }
193