test-work_week.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 //#cmake:add-file ../../../common/utilities/time/work_week.h
5 //#cmake:add-file ../../../common/utilities/time/work_week.cpp
6 
7 
8 #include "common.h"
9 #include "../common/utilities/time/work_week.h"
10 #include <ctime>
11 
12 using namespace utilities::time;
13 
14 // Test description:
15 // > Test the c'tor
16 TEST_CASE( "test work_week c'tor", "[work_week]" )
17 {
18  tm set_time = { 0 };
19  set_time.tm_year = 117; // 2017
20  set_time.tm_mday = 1;
21  set_time.tm_isdst = -1;
22  // The work week of the first day of the year is 1 because work weeks are 1 based
23  work_week first_day( std::mktime( &set_time ) );
24  CHECK( first_day.get_work_week() == 1 );
25  set_time.tm_mday = 7;
26  set_time.tm_wday = 6;
27  // This should be the last day of the first work week because 2017 started on Sunday
28  work_week sixth_day( std::mktime( &set_time ) );
29  CHECK( sixth_day.get_work_week() == 1 );
30  set_time.tm_mday = 8;
31  set_time.tm_wday = 0;
32  // This should be the first day of the second work week
33  work_week seventh_day( std::mktime( &set_time ) );
34  CHECK( seventh_day.get_work_week() == 2 );
35  // If the year didn't start on a Sunday (for example 2018), Jan 7th should be in work week 2
36  set_time.tm_year = 118; // 2018
37  set_time.tm_mday = 7;
38  work_week second_week( std::mktime( &set_time ) );
39  CHECK( second_week.get_work_week() == 2 );
40 
41  // Checking valid and invalid wprk weeks
42  CHECK_THROWS_WITH( work_week( 2020, 0 ),
43  "Invalid work week given: 2020 doesn't have a work week 0" );
44  CHECK_THROWS_WITH( work_week( 2020, 53 ),
45  "Invalid work week given: 2020 doesn't have a work week 53" );
46  CHECK_NOTHROW( work_week( 2016, 53 ) );
47 
48  // checking edge case of 31st of December is actualy part of work week 1 of the next year
49  set_time.tm_year = 117; // 2017
50  set_time.tm_mon = 11; // December (it's 0 based)
51  set_time.tm_mday = 31;
52  work_week belons_to_next_year( std::mktime( &set_time ) );
53  CHECK( belons_to_next_year.get_work_week() == 1 );
54  CHECK( belons_to_next_year.get_year() == 2018 );
55 }
56 
57 // Test description:
58 // > Test all work_week creation options are equivalent
59 TEST_CASE( "test work_week c'tor equivalence", "[work_week]" )
60 {
61  // Compare current with c'tor using current time
62  auto current_ww = work_week::current();
63  auto now = std::time( nullptr );
64  work_week now_to_work_week( now );
65  CHECK( current_ww.get_year() == now_to_work_week.get_year() );
66  CHECK( current_ww.get_work_week() == now_to_work_week.get_work_week() );
67 
68  // Test copy c'tor
69  work_week copy( current_ww );
70  CHECK( current_ww.get_year() == copy.get_year() );
71  CHECK( current_ww.get_work_week() == copy.get_work_week() );
72 
73  // Compare manual c'tor with c'tor from given time
74  tm set_time = { 0 };
75  set_time.tm_year = 117; // 2017
76  set_time.tm_mday = 1;
77  set_time.tm_isdst = -1;
78  work_week manual( 2017, 1 );
79  work_week from_time( std::mktime( &set_time ) );
80  CHECK( manual.get_year() == from_time.get_year() );
81  CHECK( manual.get_work_week() == from_time.get_work_week() );
82 }
83 
84 // Test description:
85 // > Test the subtraction operator for work_week
86 TEST_CASE( "test work_week subtraction", "[work_week]" )
87 {
88  // Simple cases
89  CHECK( work_week( 2019, 7 ) - work_week( 2018, 7 ) == 52 );
90  CHECK( work_week( 2019, 7 ) - work_week( 2018, 3 ) == 56 );
91  CHECK( work_week( 2019, 7 ) - work_week( 2018, 10 ) == 49 );
92 
93  // Simple cases with negative results
94  CHECK( work_week( 2018, 7 ) - work_week( 2019, 7 ) == -52 );
95  CHECK( work_week( 2018, 3 ) - work_week( 2019, 7 ) == -56 );
96  CHECK( work_week( 2018, 10 ) - work_week( 2019, 7 ) == -49 );
97 
98  // 2016 had 53 work weeks
99  CHECK( work_week( 2017, 7 ) - work_week( 2016, 7 ) == 53 );
100  CHECK( work_week( 2017, 7 ) - work_week( 2016, 3 ) == 57 );
101  CHECK( work_week( 2017, 7 ) - work_week( 2016, 10 ) == 50 );
102  CHECK( work_week( 2017, 1 ) - work_week( 2016, 52 ) == 2 );
103 
104  // Subtraction of consecutive weeks
105  CHECK( work_week( 2017, 1 ) - work_week( 2016, 53 ) == 1 );
106  tm Jan_1_2017 = { 0 };
107  Jan_1_2017.tm_year = 117; // 2017
108  Jan_1_2017.tm_mon = 0; // January (it's 0 based)
109  Jan_1_2017.tm_mday = 1;
110  Jan_1_2017.tm_wday = 0;
111  Jan_1_2017.tm_yday = 0;
112  Jan_1_2017.tm_isdst = -1;
113  tm Dec_31_2016 = { 0 };
114  Dec_31_2016.tm_year = 116; // 2016
115  Dec_31_2016.tm_mon = 11; // December (it's 0 based)
116  Dec_31_2016.tm_mday = 31;
117  Dec_31_2016.tm_wday = 5;
118  Dec_31_2016.tm_yday = 365;
119  Dec_31_2016.tm_isdst = -1;
120  CHECK( work_week( std::mktime( &Jan_1_2017 ) ) - work_week( std::mktime( &Dec_31_2016 ) )
121  == 1 );
122 
123  // Subtracting days from the same work week. December 31st 2017 is part of work week 1 of 2018
124  tm Jan_1_2018 = { 0 };
125  Jan_1_2018.tm_year = 118; // 2018
126  Jan_1_2018.tm_mon = 0; // January (it's 0 based)
127  Jan_1_2018.tm_mday = 1;
128  Jan_1_2018.tm_isdst = -1;
129  tm Dec_31_2017 = { 0 };
130  Dec_31_2017.tm_year = 117; // 2017
131  Dec_31_2017.tm_mon = 11; // December (it's 0 based)
132  Dec_31_2017.tm_mday = 31;
133  Dec_31_2017.tm_isdst = -1;
134  CHECK( work_week( std::mktime( &Jan_1_2018 ) ) - work_week( std::mktime( &Dec_31_2017 ) )
135  == 0 );
136 }
137 
138 // Test description:
139 // > Test the get_work_weeks_since function
140 TEST_CASE( "test get_work_weeks_since", "[work_week]" )
141 {
143 
144  work_week zero( 0, 1 );
145  CHECK( get_work_weeks_since( zero ) == work_week::current() - zero );
146 }
#define CHECK_THROWS_WITH(expr, matcher)
Definition: catch.hpp:17417
TEST_CASE("test work_week c'tor","[work_week]")
unsigned get_year() const
Definition: work_week.cpp:72
unsigned get_work_weeks_since(const work_week &start)
Definition: work_week.cpp:93
#define CHECK_NOTHROW(...)
Definition: catch.hpp:17421
static work_week current()
Definition: work_week.cpp:87
unsigned get_work_week() const
Definition: work_week.cpp:77
#define CHECK(condition)
void copy(void *dst, void const *src, size_t size)
Definition: types.cpp:836


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11