unix-time-test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "test_common.h"
  2. TEST_START
  3. #include "time.h"
  4. extern "C" {
  5. typedef struct tm _tm;
  6. extern int64_t time_mktime(const _tm* this_tm, int locale);
  7. extern void time_gmtime(double unix_time, _tm* this_tm);
  8. extern void time_asctime(const _tm* this_tm);
  9. void time_struct_format(const _tm* this_tm, char* str);
  10. }
  11. extern PikaMemInfo g_PikaMemInfo;
  12. /* the log_buff of printf */
  13. extern char log_buff[LOG_BUFF_MAX][LOG_SIZE];
  14. #if 0
  15. TEST(unix_time, time) {
  16. /* init */
  17. g_PikaMemInfo.heapUsedMax = 0;
  18. /* run */
  19. PikaObj* self = newRootObj("pikaMain", New_PikaMain);
  20. extern unsigned char pikaModules_py_a[];
  21. obj_linkLibrary(self, pikaModules_py_a);
  22. obj_run(self,
  23. "import time\n"
  24. "t1= time.time()\n"
  25. "t2= time.time()\n");
  26. /* 获取数据比对 */
  27. float t1 = obj_getFloat(self, "t1");
  28. float t2 = obj_getFloat(self, "t2");
  29. /* assert */
  30. EXPECT_FLOAT_EQ(0.05, t2 - t1);
  31. /* deinit */
  32. obj_deinit(self);
  33. EXPECT_EQ(pikaMemNow(), 0);
  34. }
  35. #endif
  36. int compare(const _tm* t1, const _tm* t2) {
  37. int size = 8; // 只比对前面8个数据
  38. int* it1 = (int*)t1;
  39. int* it2 = (int*)t2;
  40. for (int i = 0; i < size; i++) {
  41. // printf("t1=%d,t2=%d\n",it1[i],it2[i]);
  42. if (it1[i] != it2[i]) {
  43. // printf("mytime: ");
  44. // time_asctime(t1);
  45. // printf("ctime: ");
  46. // time_asctime(t2);
  47. return 1;
  48. }
  49. }
  50. return 0;
  51. }
  52. #if PIKA_FLOAT_TYPE_DOUBLE
  53. TEST(unix_time, iteration_form_1970_to_2070) {
  54. /* init */
  55. _tm temp1, *temp2;
  56. int64_t tint1;
  57. int64_t r = 123456;
  58. int flag = 1;
  59. char str[200];
  60. /* run */
  61. /* 获取数据比对 */
  62. int test_num = 365 * 100;
  63. int record = test_num;
  64. while (test_num--) {
  65. // r=randL2();
  66. r += 24 * 60 * 60;
  67. time_gmtime(r, &temp1);
  68. tint1 = time_mktime(&temp1, 0);
  69. temp2 = gmtime(&r);
  70. temp1.tm_yday -= 1;
  71. temp1.tm_isdst = 0;
  72. temp2->tm_year += 1900;
  73. if (compare(&temp1, temp2)) {
  74. printf("error!\n");
  75. // 格式化字符
  76. time_struct_format(&temp1, str);
  77. printf("%s\n", str);
  78. time_struct_format(temp2, str);
  79. printf("%s\n", str);
  80. flag = 0;
  81. break;
  82. }
  83. if (tint1 != r) {
  84. printf("\n error!tint1 = %ld ,r = %ld \n", tint1, r);
  85. flag = 0;
  86. break;
  87. }
  88. // printf("\n\n");
  89. }
  90. printf("Had passed %d times test !\r\n", record - test_num - 1);
  91. /* assert */
  92. EXPECT_EQ(flag, 1);
  93. /* deinit */
  94. }
  95. #endif
  96. TEST(timetest, sleep) {
  97. char* lines =
  98. "import time\n"
  99. "t = PikaStdDevice.Time()\n"
  100. "t.sleep(0.1)\n";
  101. /* init */
  102. g_PikaMemInfo.heapUsedMax = 0;
  103. PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
  104. extern unsigned char pikaModules_py_a[];
  105. obj_linkLibrary(pikaMain, pikaModules_py_a);
  106. /* run */
  107. __platform_printf("BEGIN\r\n");
  108. obj_run(pikaMain, lines);
  109. /* collect */
  110. /* assert */
  111. /* deinit */
  112. obj_deinit(pikaMain);
  113. EXPECT_EQ(pikaMemNow(), 0);
  114. }
  115. #if PIKA_STD_DEVICE_UNIX_TIME_ENABLE && PIKA_FLOAT_TYPE_DOUBLE
  116. TEST_RUN_SINGLE_FILE_EXCEPT_OUTPUT(time,
  117. test1,
  118. "test/python/time/time_test1.py",
  119. "PASS\r\n")
  120. #endif
  121. TEST_END