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