init

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * This is the mbed device part of the test to verify if:
00020  * - _rtc_maketime() function converts a calendar time into time since UNIX epoch as a time_t,
00021  * - _rtc_localtime() function converts a given time in seconds since epoch into calendar time.
00022  */
00023 
00024 #include "mbed.h"
00025 #include "greentea-client/test_env.h"
00026 #include "utest/utest.h"
00027 #include "unity/unity.h"
00028 #include "mbed_mktime.h"
00029 
00030 #define LAST_VALID_YEAR 206
00031 
00032 using namespace utest::v1;
00033 
00034 static rtc_leap_year_support_t rtc_leap_year_support;
00035 
00036 /*
00037  * regular is_leap_year, see platform/mbed_mktime.c for the optimised version
00038  */
00039 bool is_leap_year(int year)
00040 {
00041     year = 1900 + year;
00042     if (year % 4) {
00043         return false;
00044     } else if (year % 100) {
00045         return true;
00046     } else if (year % 400) {
00047         return false;
00048     }
00049     return true;
00050 }
00051 
00052 struct tm make_time_info(int year, int month, int day, int hours, int minutes, int seconds)
00053 {
00054     struct tm timeinfo =
00055     { seconds,    // tm_sec
00056         minutes,    // tm_min
00057         hours,      // tm_hour
00058         day,        // tm_mday
00059         month,      // tm_mon
00060         year,       // tm_year
00061         0,          // tm_wday
00062         0,          // tm_yday
00063         0,          // tm_isdst
00064             };
00065     return timeinfo;
00066 }
00067 
00068 /* Test _rtc_maketime() and _rtc_localtime() across wide range
00069  *
00070  * Note: This test functions handles both types of RTC devices:
00071  * - devices which supports full leap years support in range 1970 - 2106.
00072  * - devices which supports parial leap years support and incorrectly treats 2100 year as a leap year.
00073  *
00074  * Given is valid calendar time.
00075  * When _rtc_maketime() is used to generate timestamp from calendar time and _rtc_localtime() is used to convert
00076  * timestamp to calendar time.
00077  * Then both operations gives valid results.
00078  */
00079 void test_case_mktime_localtime()
00080 {
00081     char _key[11] =
00082     { };
00083     char _value[128] =
00084     { };
00085 
00086     size_t years[] = {70, 71, 100, 196, 200, 205};
00087 
00088     /* Inform host part of the test about tested RTC type. */
00089     greentea_send_kv("leap_year_setup",  rtc_leap_year_support);
00090 
00091     /* Check the first and last last day of each month. */
00092     for (size_t year_id = 0; year_id < (sizeof(years) /sizeof(size_t)) ; ++year_id) {
00093         for (size_t month = 0; month < 12; ++month) {
00094             for (size_t dayid = 0; dayid < 2; ++dayid) {
00095 
00096                 size_t year = years[year_id];
00097 
00098                 size_t day = 0;
00099                 /* Test the first and the last day of each month:
00100                  * day 0 - first,
00101                  * day 1 - last
00102                  * */
00103                 switch (dayid)
00104                 {
00105                     case 0:
00106                         day = 1;
00107                         break;
00108 
00109                     case 1:
00110                         day = 31;
00111 
00112                         if (month == 3 || month == 5 || month == 8 || month == 10) {
00113                             day = 30;
00114                         }
00115 
00116                         if (month == 1) {
00117                             day = 28;
00118                         }
00119 
00120                         if (month == 1 && is_leap_year(year)) {
00121                             day = 29;
00122                         }
00123 
00124                         /* Additional conditions for RTCs with partial leap year support. */
00125                         if(month == 1 && year == 200 && rtc_leap_year_support == RTC_4_YEAR_LEAP_YEAR_SUPPORT) {
00126                             day = 29;
00127                         }
00128 
00129                         break;
00130 
00131                     default:
00132                         break;
00133                 }
00134 
00135                 tm time_info = make_time_info(year, month, day, 23, dayid ? 59 : 0, dayid ? 59 : 0);
00136 
00137                 time_t actual_timestamp;
00138 
00139                 TEST_ASSERT_TRUE(_rtc_maketime(&time_info, &actual_timestamp, rtc_leap_year_support));
00140 
00141                 greentea_send_kv("timestamp", (int) actual_timestamp);
00142 
00143                 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
00144 
00145                 TEST_ASSERT_EQUAL_STRING("passed", _key);
00146 
00147                 /* Response which indicates success contains encoded week day
00148                  * and year day needed to verify _rtc_localtime().
00149                  * Use validated timestamp to generate and validate calendar time.
00150                  */
00151 
00152                 unsigned int buf = (unsigned int) strtol(_value, NULL, 10);
00153 
00154                 time_info.tm_wday = ((buf >> 16) & 0x0000FFFF);
00155                 time_info.tm_yday = (buf & 0x0000FFFF);
00156 
00157                 tm actual_time_info;
00158 
00159                 bool result = _rtc_localtime((time_t) actual_timestamp, &actual_time_info, rtc_leap_year_support);
00160 
00161                 TEST_ASSERT_TRUE(result);
00162                 TEST_ASSERT_EQUAL_UINT32_MESSAGE(time_info.tm_sec, actual_time_info.tm_sec, "invalid seconds");
00163                 TEST_ASSERT_EQUAL_UINT32_MESSAGE(time_info.tm_min, actual_time_info.tm_min, "invalid minutes");
00164                 TEST_ASSERT_EQUAL_UINT32_MESSAGE(time_info.tm_hour, actual_time_info.tm_hour, "invalid hours");
00165                 TEST_ASSERT_EQUAL_UINT32_MESSAGE(time_info.tm_mday, actual_time_info.tm_mday, "invalid day");
00166                 TEST_ASSERT_EQUAL_UINT32_MESSAGE(time_info.tm_mon, actual_time_info.tm_mon, "invalid month");
00167                 TEST_ASSERT_EQUAL_UINT32_MESSAGE(time_info.tm_year, actual_time_info.tm_year, "invalid year");
00168                 TEST_ASSERT_EQUAL_UINT32_MESSAGE(time_info.tm_wday, actual_time_info.tm_wday, "invalid weekday");
00169                 TEST_ASSERT_EQUAL_UINT32_MESSAGE(time_info.tm_yday, actual_time_info.tm_yday, "invalid year day");
00170             }
00171         }
00172     }
00173 }
00174 
00175 utest::v1::status_t full_leap_year_case_setup_handler_t(const Case * const source, const size_t index_of_case)
00176 {
00177     rtc_leap_year_support = RTC_FULL_LEAP_YEAR_SUPPORT;
00178 
00179     return greentea_case_setup_handler(source, index_of_case);
00180 }
00181 
00182 utest::v1::status_t partial_leap_year_case_setup_handler_t(const Case * const source, const size_t index_of_case)
00183 {
00184     rtc_leap_year_support = RTC_4_YEAR_LEAP_YEAR_SUPPORT;
00185 
00186     return greentea_case_setup_handler(source, index_of_case);
00187 }
00188 
00189 utest::v1::status_t teardown_handler_t(const Case * const source, const size_t passed, const size_t failed,
00190         const failure_t reason)
00191 {
00192     return greentea_case_teardown_handler(source, passed, failed, reason);
00193 }
00194 
00195 // Test cases
00196 Case cases[] ={
00197    Case("test make time and local time - RTC leap years full support", full_leap_year_case_setup_handler_t, test_case_mktime_localtime, teardown_handler_t),
00198    Case("test make time and local time - RTC leap years partial support", partial_leap_year_case_setup_handler_t, test_case_mktime_localtime, teardown_handler_t),
00199 };
00200 
00201 utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
00202 {
00203     GREENTEA_SETUP(300, "rtc_calc_auto");
00204     return greentea_test_setup_handler(number_of_cases);
00205 }
00206 
00207 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
00208 
00209 int main()
00210 {
00211     Harness::run(specification);
00212 }