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 <zircon/device/rtc.h> 7#include <librtc.h> 8#include <stdint.h> 9 10rtc_t make_rtc(uint16_t year, uint8_t month, 11 uint8_t day, uint8_t hours, 12 uint8_t minutes, uint8_t seconds) { 13 return rtc_t { seconds, minutes, hours, day, month, year }; 14} 15 16bool santitize_rtc_test() { 17 BEGIN_TEST; 18 19 auto t0 = make_rtc(1999, 1, 1, 0, 0, 0); 20 EXPECT_TRUE(rtc_is_invalid(&t0)); 21 22 t0.year = 2000; 23 EXPECT_FALSE(rtc_is_invalid(&t0)); 24 25 t0.month = 13; 26 EXPECT_TRUE(rtc_is_invalid(&t0)); 27 t0.month = 1; 28 t0.day = 32; 29 EXPECT_TRUE(rtc_is_invalid(&t0)); 30 t0.day = 1; 31 t0.hours = 25; 32 EXPECT_TRUE(rtc_is_invalid(&t0)); 33 t0.hours = 1; 34 t0.minutes = 61; 35 EXPECT_TRUE(rtc_is_invalid(&t0)); 36 t0.minutes = 1; 37 t0.seconds = 61; 38 EXPECT_TRUE(rtc_is_invalid(&t0)); 39 40 END_TEST; 41} 42 43bool seconds_since_epoc_test() { 44 BEGIN_TEST; 45 46 auto t0 = make_rtc(2018, 8, 4, 1, 19, 1); 47 EXPECT_EQ(1533345541, seconds_since_epoch(&t0)); 48 49 auto t1 = make_rtc(2000, 1, 1, 0, 0, 0); 50 EXPECT_EQ(946684800, seconds_since_epoch(&t1)); 51 52 END_TEST; 53} 54 55BEGIN_TEST_CASE(rtc_lib_tests) 56RUN_TEST(santitize_rtc_test) 57RUN_TEST(seconds_since_epoc_test) 58END_TEST_CASE(rtc_lib_tests) 59 60int main(int argc, char** argv) { 61 return unittest_run_all_tests(argc, argv) ? 0 : -1; 62} 63