Home | History | Annotate | Download | only in calendar
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 // This test serves the purpose of making sure all the different implementations
     18 // of the unspoken CalendarLib interface support the same methods.
     19 
     20 #include "util/calendar/calendar.h"
     21 #include "util/base/logging.h"
     22 
     23 #include "gtest/gtest.h"
     24 
     25 namespace libtextclassifier2 {
     26 namespace {
     27 
     28 TEST(CalendarTest, Interface) {
     29   CalendarLib calendar;
     30   int64 time;
     31   std::string timezone;
     32   bool result = calendar.InterpretParseData(
     33       DateParseData{0l, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     34                     static_cast<DateParseData::Relation>(0),
     35                     static_cast<DateParseData::RelationType>(0), 0},
     36       0L, "Zurich", "en-CH", GRANULARITY_UNKNOWN, &time);
     37   TC_LOG(INFO) << result;
     38 }
     39 
     40 #ifdef LIBTEXTCLASSIFIER_UNILIB_ICU
     41 TEST(CalendarTest, RoundingToGranularity) {
     42   CalendarLib calendar;
     43   int64 time;
     44   std::string timezone;
     45   DateParseData data;
     46   data.year = 2018;
     47   data.month = 4;
     48   data.day_of_month = 25;
     49   data.hour = 9;
     50   data.minute = 33;
     51   data.second = 59;
     52   data.field_set_mask = DateParseData::YEAR_FIELD | DateParseData::MONTH_FIELD |
     53                         DateParseData::DAY_FIELD | DateParseData::HOUR_FIELD |
     54                         DateParseData::MINUTE_FIELD |
     55                         DateParseData::SECOND_FIELD;
     56   ASSERT_TRUE(calendar.InterpretParseData(
     57       data,
     58       /*reference_time_ms_utc=*/0L, /*reference_timezone=*/"Europe/Zurich",
     59       /*reference_locale=*/"en-CH",
     60       /*granularity=*/GRANULARITY_YEAR, &time));
     61   EXPECT_EQ(time, 1514761200000L /* Jan 01 2018 00:00:00 */);
     62 
     63   ASSERT_TRUE(calendar.InterpretParseData(
     64       data,
     65       /*reference_time_ms_utc=*/0L, /*reference_timezone=*/"Europe/Zurich",
     66       /*reference_locale=*/"en-CH",
     67       /*granularity=*/GRANULARITY_MONTH, &time));
     68   EXPECT_EQ(time, 1522533600000L /* Apr 01 2018 00:00:00 */);
     69 
     70   ASSERT_TRUE(calendar.InterpretParseData(
     71       data,
     72       /*reference_time_ms_utc=*/0L, /*reference_timezone=*/"Europe/Zurich",
     73       /*reference_locale=*/"en-CH",
     74       /*granularity=*/GRANULARITY_WEEK, &time));
     75   EXPECT_EQ(time, 1524434400000L /* Mon Apr 23 2018 00:00:00 */);
     76 
     77   ASSERT_TRUE(calendar.InterpretParseData(
     78       data,
     79       /*reference_time_ms_utc=*/0L, /*reference_timezone=*/"Europe/Zurich",
     80       /*reference_locale=*/"*-CH",
     81       /*granularity=*/GRANULARITY_WEEK, &time));
     82   EXPECT_EQ(time, 1524434400000L /* Mon Apr 23 2018 00:00:00 */);
     83 
     84   ASSERT_TRUE(calendar.InterpretParseData(
     85       data,
     86       /*reference_time_ms_utc=*/0L, /*reference_timezone=*/"Europe/Zurich",
     87       /*reference_locale=*/"en-US",
     88       /*granularity=*/GRANULARITY_WEEK, &time));
     89   EXPECT_EQ(time, 1524348000000L /* Sun Apr 22 2018 00:00:00 */);
     90 
     91   ASSERT_TRUE(calendar.InterpretParseData(
     92       data,
     93       /*reference_time_ms_utc=*/0L, /*reference_timezone=*/"Europe/Zurich",
     94       /*reference_locale=*/"*-US",
     95       /*granularity=*/GRANULARITY_WEEK, &time));
     96   EXPECT_EQ(time, 1524348000000L /* Sun Apr 22 2018 00:00:00 */);
     97 
     98   ASSERT_TRUE(calendar.InterpretParseData(
     99       data,
    100       /*reference_time_ms_utc=*/0L, /*reference_timezone=*/"Europe/Zurich",
    101       /*reference_locale=*/"en-CH",
    102       /*granularity=*/GRANULARITY_DAY, &time));
    103   EXPECT_EQ(time, 1524607200000L /* Apr 25 2018 00:00:00 */);
    104 
    105   ASSERT_TRUE(calendar.InterpretParseData(
    106       data,
    107       /*reference_time_ms_utc=*/0L, /*reference_timezone=*/"Europe/Zurich",
    108       /*reference_locale=*/"en-CH",
    109       /*granularity=*/GRANULARITY_HOUR, &time));
    110   EXPECT_EQ(time, 1524639600000L /* Apr 25 2018 09:00:00 */);
    111 
    112   ASSERT_TRUE(calendar.InterpretParseData(
    113       data,
    114       /*reference_time_ms_utc=*/0L, /*reference_timezone=*/"Europe/Zurich",
    115       /*reference_locale=*/"en-CH",
    116       /*granularity=*/GRANULARITY_MINUTE, &time));
    117   EXPECT_EQ(time, 1524641580000 /* Apr 25 2018 09:33:00 */);
    118 
    119   ASSERT_TRUE(calendar.InterpretParseData(
    120       data,
    121       /*reference_time_ms_utc=*/0L, /*reference_timezone=*/"Europe/Zurich",
    122       /*reference_locale=*/"en-CH",
    123       /*granularity=*/GRANULARITY_SECOND, &time));
    124   EXPECT_EQ(time, 1524641639000 /* Apr 25 2018 09:33:59 */);
    125 }
    126 #endif  // LIBTEXTCLASSIFIER_UNILIB_DUMMY
    127 
    128 }  // namespace
    129 }  // namespace libtextclassifier2
    130