edits
[iotcloud.git] / version2 / src / C / Test.ino
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 TimingSingleton *timer;
9 bool foundError;
10 MyVector<TransactionStatus *> * transStatusList;
11 Table *t1;
12 Table *t2;
13
14 void setup() {
15         timer = TimingSingleton_getInstance();
16         foundError = false;
17         transStatusList = new MyVector<TransactionStatus *>();
18         // Setup the 2 clients
19         IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
20         IoTString * password = new IoTString("reallysecret");
21         t1 = new Table(baseurl, password, 321, -1);
22         t1->initTable();
23
24         t2 = new Table(baseurl, password, 351, -1);
25         t2->update();
26
27         delete baseurl; delete password;
28
29         // Make the Keys
30         for (int i = 0; i < NUMBER_OF_TESTS; 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
51
52 void loop() {
53         
54         // Do Updates for the keys
55         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
56                 char buffer[80];
57                 sprintf(buffer, "a%d", i);
58                 IoTString * iKeyA = new IoTString(buffer);
59                 IoTString * iValueA = new IoTString(buffer);
60
61                 sprintf(buffer, "b%d", i);
62                 IoTString * iKeyB = new IoTString(buffer);
63                 IoTString * iValueB = new IoTString(buffer);
64                 
65                 sprintf(buffer, "c%d", i);
66                 IoTString * iKeyC = new IoTString(buffer);
67                 IoTString * iValueC = new IoTString(buffer);
68                 
69                 sprintf(buffer, "d%d", i);
70                 IoTString * iKeyD = new IoTString(buffer);
71                 IoTString * iValueD = new IoTString(buffer);
72
73                 t1->startTransaction();
74                 t1->put(iKeyA, iValueA);
75                 transStatusList->add(t1->commitTransaction());
76                 delete iKeyA; delete iValueA;
77                 
78                 t1->startTransaction();
79                 t1->put(iKeyB, iValueB);
80                 transStatusList->add(t1->commitTransaction());
81                 delete iKeyB; delete iValueB;
82                 
83                 t2->startTransaction();
84                 t2->put(iKeyC, iValueC);
85                 transStatusList->add(t2->commitTransaction());
86                 delete iKeyC; delete iValueC;
87                 
88                 t2->startTransaction();
89                 t2->put(iKeyD, iValueD);
90                 transStatusList->add(t2->commitTransaction());
91                 delete iKeyD; delete iValueD;
92         }
93         
94         t1->update();
95         t2->update();
96         
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         }
187
188         transStatusList->clear();
189 }
190