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