![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/mbed_hal/rtc_time/main.cpp@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2017 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 5 | * you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 6 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 9 | * |
kadonotakashi | 0:8fdf9a60065b | 10 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 13 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 14 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 15 | */ |
kadonotakashi | 0:8fdf9a60065b | 16 | |
kadonotakashi | 0:8fdf9a60065b | 17 | #include "utest/utest.h" |
kadonotakashi | 0:8fdf9a60065b | 18 | #include "unity/unity.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "mbed_mktime.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | |
kadonotakashi | 0:8fdf9a60065b | 24 | #define LAST_VALID_YEAR 206 |
kadonotakashi | 0:8fdf9a60065b | 25 | |
kadonotakashi | 0:8fdf9a60065b | 26 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 27 | |
kadonotakashi | 0:8fdf9a60065b | 28 | static rtc_leap_year_support_t rtc_leap_year_support; |
kadonotakashi | 0:8fdf9a60065b | 29 | |
kadonotakashi | 0:8fdf9a60065b | 30 | /* Regular is_leap_year, see platform/mbed_mktime.c for the optimised version. */ |
kadonotakashi | 0:8fdf9a60065b | 31 | bool is_leap_year(int year) |
kadonotakashi | 0:8fdf9a60065b | 32 | { |
kadonotakashi | 0:8fdf9a60065b | 33 | year = 1900 + year; |
kadonotakashi | 0:8fdf9a60065b | 34 | if (year % 4) { |
kadonotakashi | 0:8fdf9a60065b | 35 | return false; |
kadonotakashi | 0:8fdf9a60065b | 36 | } else if (year % 100) { |
kadonotakashi | 0:8fdf9a60065b | 37 | return true; |
kadonotakashi | 0:8fdf9a60065b | 38 | } else if (year % 400) { |
kadonotakashi | 0:8fdf9a60065b | 39 | return false; |
kadonotakashi | 0:8fdf9a60065b | 40 | } |
kadonotakashi | 0:8fdf9a60065b | 41 | return true; |
kadonotakashi | 0:8fdf9a60065b | 42 | } |
kadonotakashi | 0:8fdf9a60065b | 43 | |
kadonotakashi | 0:8fdf9a60065b | 44 | /* Test the optimised version of _rtc_is_leap_year() against the generic version. |
kadonotakashi | 0:8fdf9a60065b | 45 | * |
kadonotakashi | 0:8fdf9a60065b | 46 | * Note: This test case is designed for both types of RTC devices: |
kadonotakashi | 0:8fdf9a60065b | 47 | * - RTC devices which handle correctly leap years in whole range (1970 - 2106). |
kadonotakashi | 0:8fdf9a60065b | 48 | * - RTC devices which does not handle correctly leap years in whole range (1970 - 2106). |
kadonotakashi | 0:8fdf9a60065b | 49 | * This RTC devices uses simpler leap year detection and incorrectly treat 2100 as a leap year. |
kadonotakashi | 0:8fdf9a60065b | 50 | * rtc_leap_year_support variable specifies which device is tested. |
kadonotakashi | 0:8fdf9a60065b | 51 | * |
kadonotakashi | 0:8fdf9a60065b | 52 | * Given is year in valid range. |
kadonotakashi | 0:8fdf9a60065b | 53 | * When _rtc_is_leap_year() function is called. |
kadonotakashi | 0:8fdf9a60065b | 54 | * Then _rtc_is_leap_year() returns true if given year is a leap year; false otherwise. |
kadonotakashi | 0:8fdf9a60065b | 55 | */ |
kadonotakashi | 0:8fdf9a60065b | 56 | void test_is_leap_year() |
kadonotakashi | 0:8fdf9a60065b | 57 | { |
kadonotakashi | 0:8fdf9a60065b | 58 | for (int i = 70; i <= LAST_VALID_YEAR; ++i) { |
kadonotakashi | 0:8fdf9a60065b | 59 | bool expected = is_leap_year(i); |
kadonotakashi | 0:8fdf9a60065b | 60 | |
kadonotakashi | 0:8fdf9a60065b | 61 | /* Add exception for year 2100. */ |
kadonotakashi | 0:8fdf9a60065b | 62 | if (rtc_leap_year_support == RTC_4_YEAR_LEAP_YEAR_SUPPORT && i == 200) { |
kadonotakashi | 0:8fdf9a60065b | 63 | expected = true; |
kadonotakashi | 0:8fdf9a60065b | 64 | } |
kadonotakashi | 0:8fdf9a60065b | 65 | |
kadonotakashi | 0:8fdf9a60065b | 66 | bool actual_value = _rtc_is_leap_year(i, rtc_leap_year_support); |
kadonotakashi | 0:8fdf9a60065b | 67 | |
kadonotakashi | 0:8fdf9a60065b | 68 | if (expected != actual_value) { |
kadonotakashi | 0:8fdf9a60065b | 69 | printf("Leap year failed with i = %d\r\n", i); |
kadonotakashi | 0:8fdf9a60065b | 70 | } |
kadonotakashi | 0:8fdf9a60065b | 71 | TEST_ASSERT_EQUAL(expected, actual_value); |
kadonotakashi | 0:8fdf9a60065b | 72 | } |
kadonotakashi | 0:8fdf9a60065b | 73 | } |
kadonotakashi | 0:8fdf9a60065b | 74 | |
kadonotakashi | 0:8fdf9a60065b | 75 | /* Structure to test border values for _rtc_maketime(). */ |
kadonotakashi | 0:8fdf9a60065b | 76 | typedef struct { |
kadonotakashi | 0:8fdf9a60065b | 77 | struct tm timeinfo; |
kadonotakashi | 0:8fdf9a60065b | 78 | time_t exp_seconds; // if result is false then exp_seconds is irrelevant |
kadonotakashi | 0:8fdf9a60065b | 79 | bool result; |
kadonotakashi | 0:8fdf9a60065b | 80 | } test_mk_time_struct; |
kadonotakashi | 0:8fdf9a60065b | 81 | |
kadonotakashi | 0:8fdf9a60065b | 82 | /* Array which contains data to test boundary values for the RTC devices which handles correctly leap years in |
kadonotakashi | 0:8fdf9a60065b | 83 | * whole range (1970 - 2106). |
kadonotakashi | 0:8fdf9a60065b | 84 | * Expected range: the 1st of January 1970 at 00:00:00 (seconds: 0) to the 7th of February 2106 at 06:28:15 (seconds: UINT_MAX). |
kadonotakashi | 0:8fdf9a60065b | 85 | */ |
kadonotakashi | 0:8fdf9a60065b | 86 | test_mk_time_struct test_mk_time_arr_full[] = { |
kadonotakashi | 0:8fdf9a60065b | 87 | {{ 0, 0, 0, 1, 0, 70, 0, 0, 0 }, (time_t) 0, true}, // valid lower bound - the 1st of January 1970 at 00:00:00 |
kadonotakashi | 0:8fdf9a60065b | 88 | {{ 59, 59, 23, 31, 11, 59, 0, 0, 0 }, (time_t) 0, false }, // invalid lower bound - the 31st of December 1969 at 23:59:59 |
kadonotakashi | 0:8fdf9a60065b | 89 | |
kadonotakashi | 0:8fdf9a60065b | 90 | {{ 15, 28, 6, 7, 1, 206, 0, 0, 0 }, (time_t)(UINT_MAX), true }, // valid upper bound - the 7th of February 2106 at 06:28:15 |
kadonotakashi | 0:8fdf9a60065b | 91 | {{ 16, 28, 6, 7, 1, 206, 0, 0, 0 }, (time_t) 0, false }, // invalid upper bound - the 7th of February 2106 at 06:28:16 |
kadonotakashi | 0:8fdf9a60065b | 92 | }; |
kadonotakashi | 0:8fdf9a60065b | 93 | |
kadonotakashi | 0:8fdf9a60065b | 94 | /* Array which contains data to test boundary values for the RTC devices which does not handle correctly leap years in |
kadonotakashi | 0:8fdf9a60065b | 95 | * whole range (1970 - 2106). On this platforms we will be one day off after 28.02.2100 since 2100 year will be |
kadonotakashi | 0:8fdf9a60065b | 96 | * incorrectly treated as a leap year. |
kadonotakashi | 0:8fdf9a60065b | 97 | * Expected range: the 1st of January 1970 at 00:00:00 (seconds: 0) to the 6th of February 2106 at 06:28:15 (seconds: UINT_MAX). |
kadonotakashi | 0:8fdf9a60065b | 98 | */ |
kadonotakashi | 0:8fdf9a60065b | 99 | test_mk_time_struct test_mk_time_arr_partial[] = { |
kadonotakashi | 0:8fdf9a60065b | 100 | {{ 0, 0, 0, 1, 0, 70, 0, 0, 0 }, (time_t) 0, true}, // valid lower bound - the 1st of January 1970 at 00:00:00 |
kadonotakashi | 0:8fdf9a60065b | 101 | {{ 59, 59, 23, 31, 11, 59, 0, 0, 0 }, (time_t) 0, false }, // invalid lower bound - the 31st of December 1969 at 23:59:59 |
kadonotakashi | 0:8fdf9a60065b | 102 | |
kadonotakashi | 0:8fdf9a60065b | 103 | {{ 15, 28, 6, 6, 1, 206, 0, 0, 0 }, (time_t)(UINT_MAX), true }, // valid upper bound - the 6th of February 2106 at 06:28:15 |
kadonotakashi | 0:8fdf9a60065b | 104 | {{ 16, 28, 6, 6, 1, 206, 0, 0, 0 }, (time_t) 0, false }, // invalid upper bound - the 6th of February 2106 at 06:28:16 |
kadonotakashi | 0:8fdf9a60065b | 105 | }; |
kadonotakashi | 0:8fdf9a60065b | 106 | |
kadonotakashi | 0:8fdf9a60065b | 107 | /* Test boundary values for _rtc_maketime(). |
kadonotakashi | 0:8fdf9a60065b | 108 | * |
kadonotakashi | 0:8fdf9a60065b | 109 | * Note: This test case is designed for both types of RTC devices: |
kadonotakashi | 0:8fdf9a60065b | 110 | * - RTC devices which handle correctly leap years in whole range (1970 - 2106). |
kadonotakashi | 0:8fdf9a60065b | 111 | * - RTC devices which does not handle correctly leap years in whole range (1970 - 2106). |
kadonotakashi | 0:8fdf9a60065b | 112 | * This RTC devices uses simpler leap year detection and incorrectly treat 2100 as a leap year. |
kadonotakashi | 0:8fdf9a60065b | 113 | * rtc_leap_year_support variable specifies which device is tested. |
kadonotakashi | 0:8fdf9a60065b | 114 | * |
kadonotakashi | 0:8fdf9a60065b | 115 | * Given is boundary calendar time. |
kadonotakashi | 0:8fdf9a60065b | 116 | * When _rtc_maketime() function is called to convert the calendar time into timestamp. |
kadonotakashi | 0:8fdf9a60065b | 117 | * Then if given calendar time is valid function returns true and conversion result, otherwise returns false. |
kadonotakashi | 0:8fdf9a60065b | 118 | */ |
kadonotakashi | 0:8fdf9a60065b | 119 | void test_mk_time_boundary() |
kadonotakashi | 0:8fdf9a60065b | 120 | { |
kadonotakashi | 0:8fdf9a60065b | 121 | test_mk_time_struct *pTestCases; |
kadonotakashi | 0:8fdf9a60065b | 122 | |
kadonotakashi | 0:8fdf9a60065b | 123 | /* Select array with test cases. */ |
kadonotakashi | 0:8fdf9a60065b | 124 | if (rtc_leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT) { |
kadonotakashi | 0:8fdf9a60065b | 125 | pTestCases = test_mk_time_arr_full; |
kadonotakashi | 0:8fdf9a60065b | 126 | } else { |
kadonotakashi | 0:8fdf9a60065b | 127 | pTestCases = test_mk_time_arr_partial; |
kadonotakashi | 0:8fdf9a60065b | 128 | } |
kadonotakashi | 0:8fdf9a60065b | 129 | |
kadonotakashi | 0:8fdf9a60065b | 130 | for (int i = 0; i < (sizeof(test_mk_time_arr_full) / (sizeof(test_mk_time_struct))); i++) { |
kadonotakashi | 0:8fdf9a60065b | 131 | time_t seconds; |
kadonotakashi | 0:8fdf9a60065b | 132 | bool result = _rtc_maketime(&pTestCases[i].timeinfo, &seconds, rtc_leap_year_support); |
kadonotakashi | 0:8fdf9a60065b | 133 | |
kadonotakashi | 0:8fdf9a60065b | 134 | TEST_ASSERT_EQUAL(pTestCases[i].result, result); |
kadonotakashi | 0:8fdf9a60065b | 135 | |
kadonotakashi | 0:8fdf9a60065b | 136 | /* If the result is false, then we have conversion error - skip checking seconds. */ |
kadonotakashi | 0:8fdf9a60065b | 137 | if (pTestCases[i].result) { |
kadonotakashi | 0:8fdf9a60065b | 138 | TEST_ASSERT_EQUAL_UINT32(pTestCases[i].exp_seconds, seconds); |
kadonotakashi | 0:8fdf9a60065b | 139 | } |
kadonotakashi | 0:8fdf9a60065b | 140 | } |
kadonotakashi | 0:8fdf9a60065b | 141 | } |
kadonotakashi | 0:8fdf9a60065b | 142 | |
kadonotakashi | 0:8fdf9a60065b | 143 | /* Test _rtc_maketime() function - call with invalid parameters. |
kadonotakashi | 0:8fdf9a60065b | 144 | * |
kadonotakashi | 0:8fdf9a60065b | 145 | * Given is _rtc_maketime() function. |
kadonotakashi | 0:8fdf9a60065b | 146 | * When _rtc_maketime() function is called with invalid parameter. |
kadonotakashi | 0:8fdf9a60065b | 147 | * Then _rtc_maketime() function returns false. |
kadonotakashi | 0:8fdf9a60065b | 148 | */ |
kadonotakashi | 0:8fdf9a60065b | 149 | void test_mk_time_invalid_param() |
kadonotakashi | 0:8fdf9a60065b | 150 | { |
kadonotakashi | 0:8fdf9a60065b | 151 | time_t seconds; |
kadonotakashi | 0:8fdf9a60065b | 152 | struct tm timeinfo; |
kadonotakashi | 0:8fdf9a60065b | 153 | |
kadonotakashi | 0:8fdf9a60065b | 154 | TEST_ASSERT_EQUAL(false, _rtc_maketime(NULL, &seconds, RTC_FULL_LEAP_YEAR_SUPPORT)); |
kadonotakashi | 0:8fdf9a60065b | 155 | TEST_ASSERT_EQUAL(false, _rtc_maketime(NULL, &seconds, RTC_4_YEAR_LEAP_YEAR_SUPPORT)); |
kadonotakashi | 0:8fdf9a60065b | 156 | TEST_ASSERT_EQUAL(false, _rtc_maketime(&timeinfo, NULL, RTC_FULL_LEAP_YEAR_SUPPORT)); |
kadonotakashi | 0:8fdf9a60065b | 157 | TEST_ASSERT_EQUAL(false, _rtc_maketime(&timeinfo, NULL, RTC_4_YEAR_LEAP_YEAR_SUPPORT)); |
kadonotakashi | 0:8fdf9a60065b | 158 | } |
kadonotakashi | 0:8fdf9a60065b | 159 | |
kadonotakashi | 0:8fdf9a60065b | 160 | /* Test _rtc_localtime() function - call with invalid parameters. |
kadonotakashi | 0:8fdf9a60065b | 161 | * |
kadonotakashi | 0:8fdf9a60065b | 162 | * Given is _rtc_localtime() function. |
kadonotakashi | 0:8fdf9a60065b | 163 | * When _rtc_localtime() function is called with invalid parameter. |
kadonotakashi | 0:8fdf9a60065b | 164 | * Then _rtc_localtime() function returns false. |
kadonotakashi | 0:8fdf9a60065b | 165 | */ |
kadonotakashi | 0:8fdf9a60065b | 166 | void test_local_time_invalid_param() |
kadonotakashi | 0:8fdf9a60065b | 167 | { |
kadonotakashi | 0:8fdf9a60065b | 168 | TEST_ASSERT_EQUAL(false, _rtc_localtime(1, NULL, RTC_FULL_LEAP_YEAR_SUPPORT)); |
kadonotakashi | 0:8fdf9a60065b | 169 | TEST_ASSERT_EQUAL(false, _rtc_localtime(1, NULL, RTC_4_YEAR_LEAP_YEAR_SUPPORT)); |
kadonotakashi | 0:8fdf9a60065b | 170 | } |
kadonotakashi | 0:8fdf9a60065b | 171 | |
kadonotakashi | 0:8fdf9a60065b | 172 | utest::v1::status_t teardown_handler_t(const Case *const source, const size_t passed, const size_t failed, |
kadonotakashi | 0:8fdf9a60065b | 173 | const failure_t reason) |
kadonotakashi | 0:8fdf9a60065b | 174 | { |
kadonotakashi | 0:8fdf9a60065b | 175 | return greentea_case_teardown_handler(source, passed, failed, reason); |
kadonotakashi | 0:8fdf9a60065b | 176 | } |
kadonotakashi | 0:8fdf9a60065b | 177 | |
kadonotakashi | 0:8fdf9a60065b | 178 | utest::v1::status_t full_leap_year_case_setup_handler_t(const Case *const source, const size_t index_of_case) |
kadonotakashi | 0:8fdf9a60065b | 179 | { |
kadonotakashi | 0:8fdf9a60065b | 180 | rtc_leap_year_support = RTC_FULL_LEAP_YEAR_SUPPORT; |
kadonotakashi | 0:8fdf9a60065b | 181 | |
kadonotakashi | 0:8fdf9a60065b | 182 | return greentea_case_setup_handler(source, index_of_case); |
kadonotakashi | 0:8fdf9a60065b | 183 | } |
kadonotakashi | 0:8fdf9a60065b | 184 | |
kadonotakashi | 0:8fdf9a60065b | 185 | utest::v1::status_t partial_leap_year_case_setup_handler_t(const Case *const source, const size_t index_of_case) |
kadonotakashi | 0:8fdf9a60065b | 186 | { |
kadonotakashi | 0:8fdf9a60065b | 187 | rtc_leap_year_support = RTC_4_YEAR_LEAP_YEAR_SUPPORT; |
kadonotakashi | 0:8fdf9a60065b | 188 | |
kadonotakashi | 0:8fdf9a60065b | 189 | return greentea_case_setup_handler(source, index_of_case); |
kadonotakashi | 0:8fdf9a60065b | 190 | } |
kadonotakashi | 0:8fdf9a60065b | 191 | |
kadonotakashi | 0:8fdf9a60065b | 192 | Case cases[] = { |
kadonotakashi | 0:8fdf9a60065b | 193 | Case("test is leap year - RTC leap years full support", full_leap_year_case_setup_handler_t, test_is_leap_year, teardown_handler_t), |
kadonotakashi | 0:8fdf9a60065b | 194 | Case("test is leap year - RTC leap years partial support", partial_leap_year_case_setup_handler_t, test_is_leap_year, teardown_handler_t), |
kadonotakashi | 0:8fdf9a60065b | 195 | Case("test make time boundary values - RTC leap years full support", full_leap_year_case_setup_handler_t, test_mk_time_boundary, teardown_handler_t), |
kadonotakashi | 0:8fdf9a60065b | 196 | Case("test make time boundary values - RTC leap years partial support", partial_leap_year_case_setup_handler_t, test_mk_time_boundary, teardown_handler_t), |
kadonotakashi | 0:8fdf9a60065b | 197 | Case("test make time - invalid param", test_mk_time_invalid_param, teardown_handler_t), |
kadonotakashi | 0:8fdf9a60065b | 198 | Case("test local time - invalid param", test_local_time_invalid_param, teardown_handler_t), |
kadonotakashi | 0:8fdf9a60065b | 199 | }; |
kadonotakashi | 0:8fdf9a60065b | 200 | |
kadonotakashi | 0:8fdf9a60065b | 201 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
kadonotakashi | 0:8fdf9a60065b | 202 | { |
kadonotakashi | 0:8fdf9a60065b | 203 | GREENTEA_SETUP(20, "default_auto"); |
kadonotakashi | 0:8fdf9a60065b | 204 | return greentea_test_setup_handler(number_of_cases); |
kadonotakashi | 0:8fdf9a60065b | 205 | } |
kadonotakashi | 0:8fdf9a60065b | 206 | |
kadonotakashi | 0:8fdf9a60065b | 207 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
kadonotakashi | 0:8fdf9a60065b | 208 | |
kadonotakashi | 0:8fdf9a60065b | 209 | int main() |
kadonotakashi | 0:8fdf9a60065b | 210 | { |
kadonotakashi | 0:8fdf9a60065b | 211 | return Harness::run(specification); |
kadonotakashi | 0:8fdf9a60065b | 212 | } |