Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / resource / c_common / ocrandom / test / arduino / randomtest.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "ocrandom.h"
22 #include "math.h"
23 #include <Arduino.h>
24
25 #define ARR_SIZE (20)
26
27 void setup() {
28     Serial.begin(115200);
29     Serial.println("Testing Random Number generator for Arduino");
30
31     Serial.print("Testing OCSeedRandom ... ");
32     if (OCSeedRandom() == 0) {
33         Serial.println("[Success]");
34     } else {
35         Serial.println("[Fail]");
36     }
37
38     Serial.print("Testing OCGetRandomByte ... ");
39     uint8_t value8 = OCGetRandomByte();
40     if (value8 >= 0 && value8 < pow(2, 8)) {
41         Serial.println("[Success]");
42     } else {
43         Serial.println("[Fail]");
44     }
45
46     Serial.print("Testing OCGetRandom ... ");
47     uint32_t value32 = OCGetRandom();
48     if (value32 >= 0 && value32 < pow(2, 32)) {
49         Serial.println("[Success]");
50     } else {
51         Serial.println("[Fail]");
52     }
53
54     Serial.print("Testing OCFillRandomMem ... ");
55     uint8_t array[ARR_SIZE] = {};
56     OCFillRandomMem(array + 1, ARR_SIZE - 2);
57     uint8_t overall = 0;
58     uint8_t value82 = 0;
59     for (int i = 1; i <= ARR_SIZE - 2; i++) {
60         value82 = array[i];
61         if (value82 >= 0 && value82 < pow(2, 8)) {
62             overall++;
63         }
64     }
65     if (overall == ARR_SIZE - 2 && array[0] == 0 && array[ARR_SIZE - 1] == 0) {
66         Serial.println("[Success]");
67     } else {
68         Serial.println("[Fail]");
69         Serial.print("overall:");Serial.println(overall);
70         Serial.print("array[0]:");Serial.println(array[0]);
71         Serial.print("array[ARR_SIZE - 1]:");Serial.println(array[ARR_SIZE - 1]);
72     }
73     Serial.println("========DONE TESTING=========");
74
75 }
76
77 void loop() {
78
79 }
80