From 555e0681be6eb894a4c513e7d77e8d0d62a40f3d Mon Sep 17 00:00:00 2001 From: vcbchang Date: Wed, 4 Aug 2021 11:47:42 +0800 Subject: [PATCH] =?UTF-8?q?test:=20clock=E7=94=A8=E4=BE=8B=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E8=B0=83=E6=95=B4=20=E3=80=90=E8=83=8C=E6=99=AF?= =?UTF-8?q?=E3=80=91=E5=BD=93=E5=89=8Dkernel=E7=9A=84unittest=E4=B8=8B?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E4=BE=8B=E5=85=A8=E9=87=8F=E4=B8=8E=E9=97=A8?= =?UTF-8?q?=E7=A6=81=E7=94=A8=E4=BE=8B=E7=BB=93=E6=9E=84=E4=B8=8D=E5=90=88?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E5=BD=93=E5=89=8D=E4=B8=8A=E4=BC=A0clock?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=90=8E=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 【修改方案】 1 原来没有测试clock模块的基本场景,这里补上基本场景作为smoke用例,并命名为clock_test_smoke.cpp 2 将原来smoke用例移到full中 3 删除与xts测试重复的用例和标记宏TEST_ON_LINUX的用例(即原来版本的clock_test_002,003,004用例) 4 原来clock_test_012用例用于测试进程的运行时间,里面存在正常场景和异常场景,现在将其分成两个用例 5 对所有用例重新排序,使得结构完整 re #I44170 Signed-off-by: vcbchang Change-Id: I30852a5f9c414668c00eddbcc9631d70818514a1 --- testsuites/unittest/time/clock/BUILD.gn | 22 +++--- .../clock/{smoke => full}/clock_test_001.cpp | 22 +++--- .../clock_test_002.cpp} | 15 ++-- .../clock_test_003.cpp} | 30 ++++---- .../clock/{smoke => full}/clock_test_004.cpp | 18 +++-- .../clock_test_005.cpp} | 22 +++--- .../clock/{smoke => full}/clock_test_006.cpp | 16 +++-- .../clock/{smoke => full}/clock_test_007.cpp | 15 ++-- .../clock/{smoke => full}/clock_test_008.cpp | 16 +++-- .../clock/{smoke => full}/clock_test_009.cpp | 14 ++-- .../unittest/time/clock/lt_clock_test.h | 9 ++- .../time/clock/smoke/clock_test_003.cpp | 65 ----------------- .../time/clock/smoke/clock_test_smoke.cpp | 69 +++++++++++++++++++ .../unittest/time/clock/time_clock_test.cpp | 49 +++++++------ 14 files changed, 202 insertions(+), 180 deletions(-) rename testsuites/unittest/time/clock/{smoke => full}/clock_test_001.cpp (78%) rename testsuites/unittest/time/clock/{smoke/clock_test_011.cpp => full/clock_test_002.cpp} (86%) rename testsuites/unittest/time/clock/{smoke/clock_test_012.cpp => full/clock_test_003.cpp} (76%) rename testsuites/unittest/time/clock/{smoke => full}/clock_test_004.cpp (83%) rename testsuites/unittest/time/clock/{smoke/clock_test_002.cpp => full/clock_test_005.cpp} (78%) rename testsuites/unittest/time/clock/{smoke => full}/clock_test_006.cpp (79%) rename testsuites/unittest/time/clock/{smoke => full}/clock_test_007.cpp (82%) rename testsuites/unittest/time/clock/{smoke => full}/clock_test_008.cpp (82%) rename testsuites/unittest/time/clock/{smoke => full}/clock_test_009.cpp (83%) delete mode 100644 testsuites/unittest/time/clock/smoke/clock_test_003.cpp create mode 100644 testsuites/unittest/time/clock/smoke/clock_test_smoke.cpp diff --git a/testsuites/unittest/time/clock/BUILD.gn b/testsuites/unittest/time/clock/BUILD.gn index 833cf0a4..c4526665 100644 --- a/testsuites/unittest/time/clock/BUILD.gn +++ b/testsuites/unittest/time/clock/BUILD.gn @@ -41,20 +41,18 @@ sources_entry = [ "time_clock_test.cpp", ] -sources_smoke = [ - "smoke/clock_test_001.cpp", - "smoke/clock_test_002.cpp", - "smoke/clock_test_003.cpp", - "smoke/clock_test_004.cpp", - "smoke/clock_test_006.cpp", - "smoke/clock_test_007.cpp", - "smoke/clock_test_008.cpp", - "smoke/clock_test_009.cpp", - "smoke/clock_test_011.cpp", - "smoke/clock_test_012.cpp", -] +sources_smoke = [ "smoke/clock_test_smoke.cpp" ] sources_full = [ + "full/clock_test_001.cpp", + "full/clock_test_002.cpp", + "full/clock_test_003.cpp", + "full/clock_test_004.cpp", + "full/clock_test_005.cpp", + "full/clock_test_006.cpp", + "full/clock_test_007.cpp", + "full/clock_test_008.cpp", + "full/clock_test_009.cpp", "full/clock_test_010.cpp", ] diff --git a/testsuites/unittest/time/clock/smoke/clock_test_001.cpp b/testsuites/unittest/time/clock/full/clock_test_001.cpp similarity index 78% rename from testsuites/unittest/time/clock/smoke/clock_test_001.cpp rename to testsuites/unittest/time/clock/full/clock_test_001.cpp index 0452ec9d..de8e2a4a 100644 --- a/testsuites/unittest/time/clock/smoke/clock_test_001.cpp +++ b/testsuites/unittest/time/clock/full/clock_test_001.cpp @@ -45,36 +45,38 @@ static int ClockTest(void) clockid_t clk = CLOCK_REALTIME; struct timespec res, tp, oldtp; int ret; + int passflag = 0; /* get clock resolution */ ret = clock_getres(clk, &res); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(res.tv_sec, CLOCK_RES_SEC, res.tv_sec); + ICUNIT_ASSERT_EQUAL(res.tv_nsec, CLOCK_RES_NSEC, res.tv_nsec); /* get current real time */ ret = clock_gettime(clk, &oldtp); + printf("The current real time: sec is %lld, nsec is %ld\n", oldtp.tv_sec, oldtp.tv_nsec); ICUNIT_ASSERT_EQUAL(ret, 0, ret); printf("sleep 2 seconds\n"); sleep(2); // 2, seconds. - tp.tv_sec = 5 * res.tv_sec; // 5, times the number of seconds. - tp.tv_nsec = 5 * res.tv_nsec; // 5, times the number of nseconds. - + tp.tv_sec = oldtp.tv_sec + 2; // 2, use for testing clock setting + tp.tv_nsec = oldtp.tv_nsec; + /* set real time */ ret = clock_settime(clk, &tp); -#if TEST_ON_LINUX - /* on linux host we need root to set clock time */ - ICUNIT_ASSERT_EQUAL(ret, -1, ret); - ICUNIT_ASSERT_EQUAL(errno, EPERM, errno); -#else + printf("Setting time: sec is %lld, nsec is %ld\n", tp.tv_sec, tp.tv_nsec); ICUNIT_ASSERT_EQUAL(ret, 0, ret); -#endif - LogPrintln("get real time clock again\n"); + printf("get real time clock again\n"); /* get current real time again */ ret = clock_gettime(clk, &tp); + printf("Obtaining the current time after setting: sec = %lld, nsec = %ld\n", tp.tv_sec, tp.tv_nsec); + passflag = (tp.tv_sec >= 2 + oldtp.tv_sec) && (tp.tv_sec <= 2 + oldtp.tv_sec + 1); // 2, use for testing clock setting ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(passflag, true, passflag); return 0; } diff --git a/testsuites/unittest/time/clock/smoke/clock_test_011.cpp b/testsuites/unittest/time/clock/full/clock_test_002.cpp similarity index 86% rename from testsuites/unittest/time/clock/smoke/clock_test_011.cpp rename to testsuites/unittest/time/clock/full/clock_test_002.cpp index bd91c26f..de50492f 100644 --- a/testsuites/unittest/time/clock/smoke/clock_test_011.cpp +++ b/testsuites/unittest/time/clock/full/clock_test_002.cpp @@ -46,7 +46,7 @@ static int ThreadClock(const char *msg, clockid_t cid) ret = clock_gettime(cid, &ts); ICUNIT_ASSERT_EQUAL(ret, 0, ret); - printf("%4jd.%03ld\n", ts.tv_sec, ts.tv_nsec / 1000000); // 1000000, 1ms. + printf("%lld.%03ld s\n", ts.tv_sec, ts.tv_nsec / 1000000); // 1000000, 1ms. return 0; } @@ -57,11 +57,6 @@ static int ClockTest(void) int ret; struct timespec ts; - /* check param invalid */ - ret = clock_gettime(-2050, &ts); // 2050, clock id. - ICUNIT_ASSERT_EQUAL(ret, -1, ret); - ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno); - ret = pthread_create(&thread, NULL, ThreadFuncTest, 0); ICUNIT_ASSERT_EQUAL(ret, 0, ret); @@ -69,9 +64,7 @@ static int ClockTest(void) sleep(1); printf("Main thread consuming some CPU time...\n"); - for (int j = 0; j < 4000000; j++) { // 4000000, The loop frequency. - getppid(); - } + usleep(400000); // 400000 delay for test /* get current pthread clockid */ ret = pthread_getcpuclockid(pthread_self(), &clockid); @@ -84,13 +77,13 @@ static int ClockTest(void) ret = pthread_getcpuclockid(thread, &clockid); ICUNIT_ASSERT_EQUAL(ret, 0, ret); - ret = ThreadClock("Subthread CPU time: 1 ", clockid); + ret = ThreadClock("Subthread CPU time: ", clockid); ICUNIT_ASSERT_EQUAL(ret, 0, ret); return 0; } -void ClockTest011(void) +void ClockTest002(void) { TEST_ADD_CASE(__FUNCTION__, ClockTest, TEST_POSIX, TEST_TIMES, TEST_LEVEL0, TEST_FUNCTION); } diff --git a/testsuites/unittest/time/clock/smoke/clock_test_012.cpp b/testsuites/unittest/time/clock/full/clock_test_003.cpp similarity index 76% rename from testsuites/unittest/time/clock/smoke/clock_test_012.cpp rename to testsuites/unittest/time/clock/full/clock_test_003.cpp index 88e098d0..d37ae98b 100644 --- a/testsuites/unittest/time/clock/smoke/clock_test_012.cpp +++ b/testsuites/unittest/time/clock/full/clock_test_003.cpp @@ -35,42 +35,46 @@ static int ClockTest(void) clockid_t clockid; struct timespec ts; int ret; + pid_t pid = 0; + int passflag = 0; - /* check param invalid */ - ret = clock_getcpuclockid(0, &clockid); - ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret); - - ret = clock_getcpuclockid(65, &clockid); // 65, pthread id. - ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret); - - /* get user process2 clockid */ - ret = clock_getcpuclockid(2, &clockid); + /* get kernel process 1 clockid */ + pid = 1; + ret = clock_getcpuclockid(1, &clockid); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(clockid, CLOCK_GET_CPU_CLOCKID(pid), clockid); /* get clockid time */ ret = clock_gettime(clockid, &ts); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + printf("Obtaining the process running time: %lld second, %ld nanosecond\n", ts.tv_sec, ts.tv_nsec); - /* get kernel process1 clockid */ - ret = clock_getcpuclockid(1, &clockid); + /* get user process 2 clockid */ + pid = 2; // 2, test the process 2 (An existent pid) + ret = clock_getcpuclockid(2, &clockid); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(clockid, CLOCK_GET_CPU_CLOCKID(pid), clockid); /* get clockid time */ ret = clock_gettime(clockid, &ts); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + printf("Obtaining the process running time: %lld second, %ld nanosecond\n", ts.tv_sec, ts.tv_nsec); /* get current process clockid */ + pid = getpid(); ret = clock_getcpuclockid(getpid(), &clockid); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(clockid, CLOCK_GET_CPU_CLOCKID(pid), clockid); /* get clockid time */ ret = clock_gettime(clockid, &ts); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + printf("Obtaining the process running time: %lld second, %ld nanosecond\n", ts.tv_sec, ts.tv_nsec); return 0; } -void ClockTest012(void) +void ClockTest003(void) { TEST_ADD_CASE(__FUNCTION__, ClockTest, TEST_POSIX, TEST_TIMES, TEST_LEVEL0, TEST_FUNCTION); -} \ No newline at end of file +} diff --git a/testsuites/unittest/time/clock/smoke/clock_test_004.cpp b/testsuites/unittest/time/clock/full/clock_test_004.cpp similarity index 83% rename from testsuites/unittest/time/clock/smoke/clock_test_004.cpp rename to testsuites/unittest/time/clock/full/clock_test_004.cpp index acff0292..5add0e14 100644 --- a/testsuites/unittest/time/clock/smoke/clock_test_004.cpp +++ b/testsuites/unittest/time/clock/full/clock_test_004.cpp @@ -30,24 +30,22 @@ */ #include "lt_clock_test.h" -static int CpuClockTest(void) +static int ClockTest(void) { - int pid = 0; - int ret; + pthread_t thread; clockid_t clockid; + int ret; struct timespec ts; - /* CLOCK_PROCESS_CPUTIME_ID if pid == 0 */ - ret = clock_getcpuclockid(pid, &clockid); - ICUNIT_ASSERT_EQUAL(ret, 0, ret); - - ret = clock_gettime(clockid, &ts); - ICUNIT_ASSERT_EQUAL(ret, 0, ret); + /* check param invalid */ + ret = clock_gettime(-2050, &ts); // 2050, clock id. + ICUNIT_ASSERT_EQUAL(ret, -1, ret); + ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno); return 0; } void ClockTest004(void) { - TEST_ADD_CASE(__FUNCTION__, CpuClockTest, TEST_POSIX, TEST_TIMES, TEST_LEVEL0, TEST_FUNCTION); + TEST_ADD_CASE(__FUNCTION__, ClockTest, TEST_POSIX, TEST_TIMES, TEST_LEVEL0, TEST_FUNCTION); } diff --git a/testsuites/unittest/time/clock/smoke/clock_test_002.cpp b/testsuites/unittest/time/clock/full/clock_test_005.cpp similarity index 78% rename from testsuites/unittest/time/clock/smoke/clock_test_002.cpp rename to testsuites/unittest/time/clock/full/clock_test_005.cpp index ae8f0abb..8db361ac 100644 --- a/testsuites/unittest/time/clock/smoke/clock_test_002.cpp +++ b/testsuites/unittest/time/clock/full/clock_test_005.cpp @@ -30,19 +30,25 @@ */ #include "lt_clock_test.h" -static int ClockMonotonicTest(void) +static int ClockTest(void) { - clockid_t clk = CLOCK_MONOTONIC; - struct timespec res, ts; + clockid_t clockid; + struct timespec ts; int ret; + pid_t pid = 0; - ret = clock_gettime(clk, &ts); - ICUNIT_ASSERT_EQUAL(ret, 0, ret); + /* check param invalid */ + ret = clock_getcpuclockid(pid, &clockid); + ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret); + + pid = 65; // 65, non existent process id. + ret = clock_getcpuclockid(pid, &clockid); + ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret); return 0; } -void ClockTest002(void) +void ClockTest005(void) { - TEST_ADD_CASE(__FUNCTION__, ClockMonotonicTest, TEST_POSIX, TEST_TIMES, TEST_LEVEL0, TEST_FUNCTION); -} \ No newline at end of file + TEST_ADD_CASE(__FUNCTION__, ClockTest, TEST_POSIX, TEST_TIMES, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/testsuites/unittest/time/clock/smoke/clock_test_006.cpp b/testsuites/unittest/time/clock/full/clock_test_006.cpp similarity index 79% rename from testsuites/unittest/time/clock/smoke/clock_test_006.cpp rename to testsuites/unittest/time/clock/full/clock_test_006.cpp index b22c35a0..1585f406 100644 --- a/testsuites/unittest/time/clock/smoke/clock_test_006.cpp +++ b/testsuites/unittest/time/clock/full/clock_test_006.cpp @@ -45,31 +45,35 @@ static int ClockTest(void) clockid_t clk = CLOCK_REALTIME_COARSE; struct timespec res, tp, oldtp; int ret; + int passflag = 0; /* get clock resolution */ ret = clock_getres(clk, &res); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(res.tv_sec, CLOCK_COARSE_RES_SEC, res.tv_sec); + ICUNIT_ASSERT_EQUAL(res.tv_nsec, CLOCK_COARSE_RES_NSEC, res.tv_nsec); /* get current real coarse time */ ret = clock_gettime(clk, &oldtp); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + printf("The current coarse real time: sec is %lld, nsec is %ld\n", oldtp.tv_sec, oldtp.tv_nsec); - LogPrintln("sleep 2 seconds\n"); - sleep(2); // 2, seconds. - - tp.tv_sec = 5 * res.tv_sec; // 5, times the number of seconds. - tp.tv_nsec = 5 * res.tv_nsec; // 5, times the number of nseconds. + tp.tv_sec = 5 * oldtp.tv_sec; // 5, times the number of seconds. + tp.tv_nsec = oldtp.tv_nsec; // 5, times the number of nseconds. /* set real coarse time */ ret = clock_settime(clk, &tp); ICUNIT_ASSERT_EQUAL(ret, -1, ret); ICUNIT_ASSERT_EQUAL(errno, EOPNOTSUPP, errno); - LogPrintln("get coarse real time clock again\n"); + printf("get coarse real time clock again\n"); /* get current real coarse time again */ ret = clock_gettime(clk, &tp); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + printf("The current coarse real time again: sec is %lld, nsec is %ld\n", tp.tv_sec, tp.tv_nsec); + passflag = (tp.tv_sec >= oldtp.tv_sec) && (tp.tv_sec <= oldtp.tv_sec + 1); + ICUNIT_ASSERT_EQUAL(passflag, 1, passflag); return 0; } diff --git a/testsuites/unittest/time/clock/smoke/clock_test_007.cpp b/testsuites/unittest/time/clock/full/clock_test_007.cpp similarity index 82% rename from testsuites/unittest/time/clock/smoke/clock_test_007.cpp rename to testsuites/unittest/time/clock/full/clock_test_007.cpp index 87d46923..ae574345 100644 --- a/testsuites/unittest/time/clock/smoke/clock_test_007.cpp +++ b/testsuites/unittest/time/clock/full/clock_test_007.cpp @@ -45,31 +45,34 @@ static int ClockTest(void) clockid_t clk = CLOCK_MONOTONIC_COARSE; struct timespec res, tp, oldtp; int ret; - + int passflag = 0; /* get clock resolution */ ret = clock_getres(clk, &res); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(res.tv_sec, CLOCK_COARSE_RES_SEC, res.tv_sec); + ICUNIT_ASSERT_EQUAL(res.tv_nsec, CLOCK_COARSE_RES_NSEC, res.tv_nsec); /* get current monotonic coarse time */ ret = clock_gettime(clk, &oldtp); ICUNIT_ASSERT_EQUAL(ret, 0, ret); - - LogPrintln("sleep 2 seconds\n"); - sleep(2); // 2, seconds. + printf("The current monotonic coarse time: sec is %lld, nsec is %ld\n", oldtp.tv_sec, oldtp.tv_nsec); tp.tv_sec = 5 * res.tv_sec; // 5, times the number of seconds. - tp.tv_nsec = 5 * res.tv_nsec; // 5, times the number of nseconds. + tp.tv_nsec = res.tv_nsec; /* set monotonic coarse time */ ret = clock_settime(clk, &tp); ICUNIT_ASSERT_EQUAL(ret, -1, ret); ICUNIT_ASSERT_EQUAL(errno, EOPNOTSUPP, errno); - LogPrintln("get coarse monotonic time clock again\n"); + printf("get coarse monotonic time clock again\n"); /* get current monotonic coarse time again */ ret = clock_gettime(clk, &tp); + passflag = (tp.tv_sec >= oldtp.tv_sec) && (tp.tv_sec <= oldtp.tv_sec + 1); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(passflag, 1, passflag); + printf("The current monotonic coarse time again: sec is %lld, nsec is %ld\n", tp.tv_sec, tp.tv_nsec); return 0; } diff --git a/testsuites/unittest/time/clock/smoke/clock_test_008.cpp b/testsuites/unittest/time/clock/full/clock_test_008.cpp similarity index 82% rename from testsuites/unittest/time/clock/smoke/clock_test_008.cpp rename to testsuites/unittest/time/clock/full/clock_test_008.cpp index a5d62225..436123af 100644 --- a/testsuites/unittest/time/clock/smoke/clock_test_008.cpp +++ b/testsuites/unittest/time/clock/full/clock_test_008.cpp @@ -34,8 +34,8 @@ #include #include #include -#include "lt_clock_test.h" #include +#include "lt_clock_test.h" /* When clock time is changed, timers for a relative interval are unaffected, * but timers for an absolute point in time are affected. @@ -45,20 +45,21 @@ static int ClockTest(void) clockid_t clk = CLOCK_MONOTONIC; struct timespec res, tp, oldtp; int ret; + int passflag = 0; /* get clock resolution */ ret = clock_getres(clk, &res); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(res.tv_sec, CLOCK_RES_SEC, res.tv_sec); + ICUNIT_ASSERT_EQUAL(res.tv_nsec, CLOCK_RES_NSEC, res.tv_nsec); /* get current monotonic time */ ret = clock_gettime(clk, &oldtp); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + printf("The current monotonic time: sec is %lld, nsec is %ld\n", oldtp.tv_sec, oldtp.tv_nsec); - LogPrintln("sleep 2 seconds\n"); - sleep(2); // 2, seconds. - - tp.tv_sec = 5 * res.tv_sec; // 5, times the number of seconds. - tp.tv_nsec = 5 * res.tv_nsec; // 5, times the number of nseconds. + tp.tv_sec = 5 * oldtp.tv_sec; // 5, times the number of seconds. + tp.tv_nsec = oldtp.tv_nsec; /* set real time */ ret = clock_settime(clk, &tp); @@ -69,7 +70,10 @@ static int ClockTest(void) /* get current monotonic time again */ ret = clock_gettime(clk, &tp); + passflag = (tp.tv_sec >= oldtp.tv_sec) && (tp.tv_sec <= oldtp.tv_sec + 1); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(passflag, 1, passflag); + printf("The current monotonic time: sec is %lld, nsec is %ld\n", tp.tv_sec, tp.tv_nsec); return 0; } diff --git a/testsuites/unittest/time/clock/smoke/clock_test_009.cpp b/testsuites/unittest/time/clock/full/clock_test_009.cpp similarity index 83% rename from testsuites/unittest/time/clock/smoke/clock_test_009.cpp rename to testsuites/unittest/time/clock/full/clock_test_009.cpp index aec17ca2..c6296625 100644 --- a/testsuites/unittest/time/clock/smoke/clock_test_009.cpp +++ b/testsuites/unittest/time/clock/full/clock_test_009.cpp @@ -45,31 +45,35 @@ static int ClockTest(void) clockid_t clk = CLOCK_MONOTONIC_RAW; struct timespec res, tp, oldtp; int ret; + int passflag = 0; /* get clock resolution */ ret = clock_getres(clk, &res); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(res.tv_sec, CLOCK_RES_SEC, res.tv_sec); + ICUNIT_ASSERT_EQUAL(res.tv_nsec, CLOCK_RES_NSEC, res.tv_nsec); /* get current monotonic raw time */ ret = clock_gettime(clk, &oldtp); ICUNIT_ASSERT_EQUAL(ret, 0, ret); - - LogPrintln("sleep 2 seconds\n"); - sleep(2); // 2, seconds. + printf("The current monotonic raw time: sec is %lld, nsec is %ld\n", oldtp.tv_sec, oldtp.tv_nsec); tp.tv_sec = 5 * res.tv_sec; // 5, times the number of seconds. - tp.tv_nsec = 5 * res.tv_nsec; // 5, times the number of nseconds. + tp.tv_nsec = res.tv_nsec; /* set monotonic raw time */ ret = clock_settime(clk, &tp); ICUNIT_ASSERT_EQUAL(ret, -1, ret); ICUNIT_ASSERT_EQUAL(errno, EOPNOTSUPP, errno); - LogPrintln("get monotonic raw time clock again\n"); + printf("get monotonic raw time clock again\n"); /* get current monotonic raw time again */ ret = clock_gettime(clk, &tp); + passflag = (tp.tv_sec >= oldtp.tv_sec) && (tp.tv_sec <= oldtp.tv_sec + 1); ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(passflag, 1, passflag); + printf("The current monotonic raw time again: sec is %lld, nsec is %ld\n", tp.tv_sec, tp.tv_nsec); return 0; } diff --git a/testsuites/unittest/time/clock/lt_clock_test.h b/testsuites/unittest/time/clock/lt_clock_test.h index c25fef3f..ac34a0dd 100644 --- a/testsuites/unittest/time/clock/lt_clock_test.h +++ b/testsuites/unittest/time/clock/lt_clock_test.h @@ -37,6 +37,13 @@ #include #include "osTest.h" +#define CLOCK_RES_SEC 0 +#define CLOCK_RES_NSEC 1000 +#define CLOCK_COARSE_RES_SEC 0 +#define CLOCK_COARSE_RES_NSEC 1000000 +#define CLOCK_GET_CPU_CLOCKID(pid) ((-pid - 1) * 8U + 2) + +void ClockTestSmoke(void); void ClockTest001(void); void ClockTest002(void); void ClockTest003(void); @@ -47,7 +54,5 @@ void ClockTest007(void); void ClockTest008(void); void ClockTest009(void); void ClockTest010(void); -void ClockTest011(void); -void ClockTest012(void); #endif /* TIME_CLOCK_LT_CLOCK_TEST_H_ */ diff --git a/testsuites/unittest/time/clock/smoke/clock_test_003.cpp b/testsuites/unittest/time/clock/smoke/clock_test_003.cpp deleted file mode 100644 index 448f8c38..00000000 --- a/testsuites/unittest/time/clock/smoke/clock_test_003.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include -#include -#include -#include -#include -#include "lt_clock_test.h" -#include - -static int ClockCoarseTest(void) -{ - clockid_t clk = CLOCK_REALTIME_COARSE; - struct timespec res, realts, monots; - int ret; - - ret = clock_gettime(clk, &realts); - ICUNIT_ASSERT_EQUAL(ret, 0, ret); - - LogPrintln("sleep 2 seconds\n"); - sleep(2); // 2, seconds. - LogPrintln("get real coarse time again\n"); - - ret = clock_gettime(clk, &realts); - ICUNIT_ASSERT_EQUAL(ret, 0, ret); - - clk = CLOCK_MONOTONIC_COARSE; - ret = clock_gettime(clk, &monots); - ICUNIT_ASSERT_EQUAL(ret, 0, ret); - - return 0; -} - -void ClockTest003(void) -{ - TEST_ADD_CASE(__FUNCTION__, ClockCoarseTest, TEST_POSIX, TEST_TIMES, TEST_LEVEL0, TEST_FUNCTION); -} \ No newline at end of file diff --git a/testsuites/unittest/time/clock/smoke/clock_test_smoke.cpp b/testsuites/unittest/time/clock/smoke/clock_test_smoke.cpp new file mode 100644 index 00000000..bde8e6ef --- /dev/null +++ b/testsuites/unittest/time/clock/smoke/clock_test_smoke.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "lt_clock_test.h" + +static int ClockSmokeTest(void) +{ + clockid_t clk = CLOCK_REALTIME; + struct timespec res = {0,0}, setts = {0,0}, oldtp = {0,0}, ts = {0,0}; + int ret; + int passflag = 0; + + /* get clock resolution */ + ret = clock_getres(clk, &res); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(res.tv_sec, CLOCK_RES_SEC, res.tv_sec); + ICUNIT_ASSERT_EQUAL(res.tv_nsec, CLOCK_RES_NSEC, res.tv_nsec); + + /* get clock realtime */ + ret = clock_gettime(clk, &oldtp); + printf("the clock current time: %lld second, %ld nanosecond\n", oldtp.tv_sec, oldtp.tv_nsec); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + /* set clock realtime */ + setts.tv_sec = oldtp.tv_sec + 1; + setts.tv_nsec = oldtp.tv_nsec; + printf("the clock setting time: %lld second, %ld nanosecond\n", setts.tv_sec, setts.tv_nsec); + ret = clock_settime(CLOCK_REALTIME, &setts); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ret = clock_gettime(clk, &ts); + printf("obtaining the current time after setting: %lld second, %ld nanosecond\n", ts.tv_sec, ts.tv_nsec); + passflag = (ts.tv_sec >= setts.tv_sec) && (ts.tv_sec <= setts.tv_sec + 1); // 1, means obtaining time's errno is 1 second. + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(passflag, true, passflag); + return 0; +} + +void ClockTestSmoke(void) +{ + TEST_ADD_CASE(__FUNCTION__, ClockSmokeTest, TEST_POSIX, TEST_TIMES, TEST_LEVEL0, TEST_FUNCTION); +} + diff --git a/testsuites/unittest/time/clock/time_clock_test.cpp b/testsuites/unittest/time/clock/time_clock_test.cpp index 43655d17..6a4bd7b6 100644 --- a/testsuites/unittest/time/clock/time_clock_test.cpp +++ b/testsuites/unittest/time/clock/time_clock_test.cpp @@ -43,6 +43,19 @@ public: }; #if defined(LOSCFG_USER_TEST_SMOKE) +/* * + * @tc.name: ClockTestSmoke + * @tc.desc: function for TimeClockTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(TimeClockTest, ClockTestSmoke, TestSize.Level0) +{ + ClockTestSmoke(); +} +#endif + +#if defined(LOSCFG_USER_TEST_FULL) /* * * @tc.name: ClockTest001 * @tc.desc: function for TimeClockTest @@ -76,7 +89,6 @@ HWTEST_F(TimeClockTest, ClockTest003, TestSize.Level0) ClockTest003(); } -#if TEST_ON_LINUX /* * * @tc.name: ClockTest004 * @tc.desc: function for TimeClockTest @@ -88,7 +100,16 @@ HWTEST_F(TimeClockTest, ClockTest004, TestSize.Level0) ClockTest004(); // clock_getcpuclockid not supported on HMOS currently } -#endif +/* * + * @tc.name: ClockTest005 + * @tc.desc: function for TimeClockTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(TimeClockTest, ClockTest005, TestSize.Level0) +{ + ClockTest005(); +} /* * * @tc.name: ClockTest006 @@ -133,9 +154,7 @@ HWTEST_F(TimeClockTest, ClockTest009, TestSize.Level0) { ClockTest009(); } -#endif -#if defined(LOSCFG_USER_TEST_FULL) /* * * @tc.name: ClockTest010 * @tc.desc: function for TimeClockTest @@ -147,27 +166,5 @@ HWTEST_F(TimeClockTest, ClockTest010, TestSize.Level0) ClockTest010(); } -/* * - * @tc.name: ClockTest011 - * @tc.desc: test pthread_getcpuclockid:get pthread time - * @tc.type: FUNC - * @tc.require: AR000E0QAB - */ -HWTEST_F(TimeClockTest, ClockTest011, TestSize.Level0) -{ - ClockTest011(); -} - -/* * - * @tc.name: ClockTest012 - * @tc.desc: test clock_getcpuclockid:get process time - * @tc.type: FUNC - * @tc.require: AR000E0QAB - */ -HWTEST_F(TimeClockTest, ClockTest012, TestSize.Level0) -{ - ClockTest012(); -} - #endif } // namespace OHOS -- GitLab