1 /* 2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited 3 */ 4 5 #include <stdio.h> 6 #include <stdint.h> 7 #include <string.h> 8 9 #include "aos/kernel.h" 10 #include "aos/hal/rng.h" 11 #include "rng_test.h" 12 13 #define RNG_TEST_TIMES 1000 14 15 random_dev_t rng_dev; 16 17 uint32_t random_buf[RNG_TEST_TIMES]; 18 hal_rng_test(void)19void hal_rng_test(void) 20 { 21 int ret = -1; 22 int i = 0; 23 int j = 0; 24 uint32_t data = 0; 25 uint32_t repeat_cnt = 0; 26 float repeat_rate = 0; 27 28 printf("*********** rng test start ! ***********\n"); 29 30 memset(random_buf, 0, sizeof(random_buf)); 31 32 rng_dev.port = PORT_RNG_TEST; 33 34 printf("step1: read random number %d times ! \n", (int)RNG_TEST_TIMES); 35 36 for (i = 0; i < RNG_TEST_TIMES; i++) 37 { 38 ret = hal_random_num_read(rng_dev, &random_buf[i], sizeof(uint32_t)); 39 if (ret != 0) { 40 printf("hal_random_num_read error !\n"); 41 return; 42 } 43 } 44 45 for (i = 0; i < RNG_TEST_TIMES; ++i) 46 { 47 for (j = i; j < (RNG_TEST_TIMES - i - 1); ++j) 48 { 49 if (random_buf[j] > random_buf[j + 1]) { 50 data = random_buf[j + 1]; 51 random_buf[j + 1] = random_buf[j]; 52 random_buf[j] = data; 53 } 54 } 55 } 56 57 for (i = 0; i < RNG_TEST_TIMES - 1; i++) 58 { 59 if (random_buf[j] == random_buf[j + 1]) { 60 repeat_cnt++; 61 } 62 63 64 printf("%x\n", random_buf[i]); 65 } 66 67 printf("step2: check the number repetition rate !\n"); 68 69 repeat_rate = (float)repeat_cnt / (float)RNG_TEST_TIMES; 70 printf("repeat_rate is %f \n", repeat_rate); 71 72 if (repeat_rate > 0.001f) { 73 printf("hal_rtc_test failed !\n"); 74 return; 75 } 76 77 printf("rng test result: succeed !\n"); 78 79 printf("*********** rng test end ! ***********\n"); 80 } 81