1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <unittest/unittest.h>
6 #include <librtc.h>
7 #include <stdint.h>
8
make_rtc(uint16_t year,uint8_t month,uint8_t day,uint8_t hours,uint8_t minutes,uint8_t seconds)9 fuchsia_hardware_rtc_Time make_rtc(uint16_t year, uint8_t month, uint8_t day, uint8_t hours,
10 uint8_t minutes, uint8_t seconds) {
11 return fuchsia_hardware_rtc_Time{seconds, minutes, hours, day, month, year};
12 }
13
santitize_rtc_test()14 bool santitize_rtc_test() {
15 BEGIN_TEST;
16
17 auto t0 = make_rtc(1999, 1, 1, 0, 0, 0);
18 EXPECT_TRUE(rtc_is_invalid(&t0));
19
20 t0.year = 2000;
21 EXPECT_FALSE(rtc_is_invalid(&t0));
22
23 t0.month = 13;
24 EXPECT_TRUE(rtc_is_invalid(&t0));
25 t0.month = 1;
26 t0.day = 32;
27 EXPECT_TRUE(rtc_is_invalid(&t0));
28 t0.day = 1;
29 t0.hours = 25;
30 EXPECT_TRUE(rtc_is_invalid(&t0));
31 t0.hours = 1;
32 t0.minutes = 61;
33 EXPECT_TRUE(rtc_is_invalid(&t0));
34 t0.minutes = 1;
35 t0.seconds = 61;
36 EXPECT_TRUE(rtc_is_invalid(&t0));
37
38 END_TEST;
39 }
40
seconds_since_epoch_test()41 bool seconds_since_epoch_test() {
42 BEGIN_TEST;
43
44 auto t0 = make_rtc(2018, 8, 4, 1, 19, 1);
45 EXPECT_EQ(1533345541, seconds_since_epoch(&t0));
46
47 auto t1 = make_rtc(2000, 1, 1, 0, 0, 0);
48 EXPECT_EQ(946684800, seconds_since_epoch(&t1));
49
50 END_TEST;
51 }
52
53 BEGIN_TEST_CASE(rtc_lib_tests)
RUN_TEST(santitize_rtc_test)54 RUN_TEST(santitize_rtc_test)
55 RUN_TEST(seconds_since_epoch_test)
56 END_TEST_CASE(rtc_lib_tests)
57
58 int main(int argc, char** argv) {
59 return unittest_run_all_tests(argc, argv) ? 0 : -1;
60 }
61