Adding Serial printouts and completing testcase for one table and two keys.
[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
13 void setup() {
14         // TODO: This test uses the Serial library
15         // Please install "screen" on your machine and run
16         // it on the serial port right after flashing the
17         // firmware onto the Particle board.
18         // e.g. sudo screen /dev/ttyACM0
19         Serial.begin();
20         timer = TimingSingleton_getInstance();
21         foundError = false;
22         transStatusList = new MyVector<TransactionStatus *>();
23         IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
24         IoTString * password = new IoTString("reallysecret");
25         t1 = new Table(baseurl, password, 321, -1);
26         t1->initTable();                                 
27         Serial.println("Finished table init...");
28         baseurl->releaseRef();
29         password->releaseRef();
30
31         // Make the Keys
32         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
33                 char buffer[80];
34                 sprintf(buffer, "a%d", i);
35                 IoTString *ia = new IoTString(buffer);
36                 sprintf(buffer, "b%d", i);
37                 IoTString *ib = new IoTString(buffer);
38                 t1->createNewKey(ia, 321);
39                 t1->createNewKey(ib, 321);
40                 ia->releaseRef();
41                 ib->releaseRef();
42         }
43         Serial.println("Finished key generation...");
44 }
45
46
47 void loop() {
48         // Do Updates for the keys
49         for (int i = 0; i < NUMBER_OF_TESTS; 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                 t1->startTransaction();
60                 t1->put(iKeyA, iValueA);
61                 transStatusList->add(t1->commitTransaction());
62                 iKeyA->releaseRef();
63                 iValueA->releaseRef();
64
65                 t1->startTransaction();
66                 t1->put(iKeyB, iValueB);
67                 transStatusList->add(t1->commitTransaction());
68                 iKeyB->releaseRef();
69                 iValueB->releaseRef();
70
71                 Serial.println("Added new transaction: ");
72                 Serial.print("a");
73                 Serial.println(i);
74                 Serial.print("b");
75                 Serial.println(i);
76         }
77
78         t1->update();
79         Serial.println("Updated table...");
80
81         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
82                 char buffer[80];
83                 sprintf(buffer, "a%d", i);
84                 IoTString * iKeyA = new IoTString(buffer);
85                 IoTString * iValueA = new IoTString(buffer);
86
87                 sprintf(buffer, "b%d", i);
88                 IoTString * iKeyB = new IoTString(buffer);
89                 IoTString * iValueB = new IoTString(buffer);
90
91                 IoTString *testValA1 = t1->getCommitted(iKeyA);
92                 IoTString *testValB1 = t1->getCommitted(iKeyB);
93
94                 if ((testValA1 == NULL) || (testValA1->equals(iValueA) == false)) {
95                         Serial.println("Key-Value t1 incorrect: keyA");
96                         foundError = true;
97                 }
98
99                 if ((testValB1 == NULL) || (testValB1->equals(iValueB) == false)) {
100                         Serial.println("Key-Value t1 incorrect: keyB");
101                         foundError = true;
102                 }
103
104                 iKeyA->releaseRef();
105                 iValueA->releaseRef();
106                 iKeyB->releaseRef();
107                 iValueB->releaseRef();
108                 testValA1->releaseRef();
109                 testValB1->releaseRef();
110         }
111
112         for (uint i = 0; i < transStatusList->size(); i++) {
113                 TransactionStatus * status = transStatusList->get(i);
114                 if (status->getStatus() != TransactionStatus_StatusCommitted) {
115                         foundError = true;
116                         Serial.println("Status error");
117                 }
118                 delete status;
119         }
120
121         if (foundError) {
122                 Serial.println("Found Errors...");
123         }
124
125         delete transStatusList;
126         delete t1;
127         
128         Serial.println("Process done... now waiting...");
129         while(true) { }
130 }