diff options
author | NAKAMURA Takumi <geek4civic@gmail.com> | 2025-01-09 18:31:57 +0900 |
---|---|---|
committer | NAKAMURA Takumi <geek4civic@gmail.com> | 2025-01-09 18:33:27 +0900 |
commit | df025ebf872052c0761d44a3ef9b65e9675af8a8 (patch) | |
tree | 9b4e94583e2536546d6606270bcdf846c95e1ba2 /libc/test/src | |
parent | 4428c9d0b1344179f85a72e183a44796976521e3 (diff) | |
parent | bdcf47e4bcb92889665825654bb80a8bbe30379e (diff) | |
download | llvm-users/chapuni/cov/single/loop.zip llvm-users/chapuni/cov/single/loop.tar.gz llvm-users/chapuni/cov/single/loop.tar.bz2 |
Merge branch 'users/chapuni/cov/single/base' into users/chapuni/cov/single/loopusers/chapuni/cov/single/loop
Conflicts:
clang/lib/CodeGen/CoverageMappingGen.cpp
Diffstat (limited to 'libc/test/src')
-rw-r--r-- | libc/test/src/stdlib/CMakeLists.txt | 18 | ||||
-rw-r--r-- | libc/test/src/stdlib/SortingTest.h | 203 | ||||
-rw-r--r-- | libc/test/src/stdlib/heap_sort_test.cpp | 18 | ||||
-rw-r--r-- | libc/test/src/stdlib/qsort_r_test.cpp | 4 | ||||
-rw-r--r-- | libc/test/src/stdlib/qsort_test.cpp | 17 | ||||
-rw-r--r-- | libc/test/src/stdlib/quick_sort_test.cpp | 19 | ||||
-rw-r--r-- | libc/test/src/time/CMakeLists.txt | 25 | ||||
-rw-r--r-- | libc/test/src/time/TmHelper.h | 9 | ||||
-rw-r--r-- | libc/test/src/time/TmMatcher.h | 3 | ||||
-rw-r--r-- | libc/test/src/time/asctime_r_test.cpp | 8 | ||||
-rw-r--r-- | libc/test/src/time/clock_gettime_test.cpp | 5 | ||||
-rw-r--r-- | libc/test/src/time/clock_test.cpp | 3 | ||||
-rw-r--r-- | libc/test/src/time/ctime_r_test.cpp | 12 | ||||
-rw-r--r-- | libc/test/src/time/difftime_test.cpp | 7 | ||||
-rw-r--r-- | libc/test/src/time/gettimeofday_test.cpp | 3 | ||||
-rw-r--r-- | libc/test/src/time/gmtime_r_test.cpp | 46 | ||||
-rw-r--r-- | libc/test/src/time/gmtime_test.cpp | 383 | ||||
-rw-r--r-- | libc/test/src/time/mktime_test.cpp | 438 | ||||
-rw-r--r-- | libc/test/src/time/nanosleep_test.cpp | 3 |
19 files changed, 695 insertions, 529 deletions
diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt index 4ca2043..8cc0428 100644 --- a/libc/test/src/stdlib/CMakeLists.txt +++ b/libc/test/src/stdlib/CMakeLists.txt @@ -301,18 +301,6 @@ add_libc_test( ) add_libc_test( - quick_sort_test - SUITE - libc-stdlib-tests - SRCS - quick_sort_test.cpp - HDRS - SortingTest.h - DEPENDS - libc.src.stdlib.qsort_util -) - -add_libc_test( heap_sort_test SUITE libc-stdlib-tests @@ -321,15 +309,15 @@ add_libc_test( HDRS SortingTest.h DEPENDS - libc.src.stdlib.qsort_util + libc.src.stdlib.qsort ) add_libc_test( - qsort_test + quick_sort_test SUITE libc-stdlib-tests SRCS - qsort_test.cpp + quick_sort_test.cpp HDRS SortingTest.h DEPENDS diff --git a/libc/test/src/stdlib/SortingTest.h b/libc/test/src/stdlib/SortingTest.h index d34584e..681a420 100644 --- a/libc/test/src/stdlib/SortingTest.h +++ b/libc/test/src/stdlib/SortingTest.h @@ -7,19 +7,19 @@ //===----------------------------------------------------------------------===// #include "src/__support/macros/config.h" -#include "src/stdlib/qsort_data.h" +#include "src/stdlib/qsort.h" #include "test/UnitTest/Test.h" class SortingTest : public LIBC_NAMESPACE::testing::Test { - using Array = LIBC_NAMESPACE::internal::Array; - using Comparator = LIBC_NAMESPACE::internal::Comparator; - using SortingRoutine = LIBC_NAMESPACE::internal::SortingRoutine; + using SortingRoutine = void (*)(void *array, size_t array_len, + size_t elem_size, + int (*compare)(const void *, const void *)); -public: static int int_compare(const void *l, const void *r) { int li = *reinterpret_cast<const int *>(l); int ri = *reinterpret_cast<const int *>(r); + if (li == ri) return 0; else if (li > ri) @@ -28,16 +28,19 @@ public: return -1; } + static void int_sort(SortingRoutine sort_func, int *array, size_t array_len) { + sort_func(reinterpret_cast<void *>(array), array_len, sizeof(int), + int_compare); + } + +public: void test_sorted_array(SortingRoutine sort_func) { int array[25] = {10, 23, 33, 35, 55, 70, 71, 100, 110, 123, 133, 135, 155, 170, 171, 1100, 1110, 1123, 1133, 1135, 1155, 1170, 1171, 11100, 12310}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); - - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_LE(array[0], 10); ASSERT_LE(array[1], 23); @@ -69,14 +72,11 @@ public: void test_reversed_sorted_array(SortingRoutine sort_func) { int array[] = {25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + int_sort(sort_func, array, ARRAY_LEN); - sort_func(arr); - - for (int i = 0; i < int(ARRAY_SIZE - 1); ++i) + for (int i = 0; i < int(ARRAY_LEN - 1); ++i) ASSERT_EQ(array[i], i + 1); } @@ -84,14 +84,11 @@ public: int array[] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); - - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); - for (size_t i = 0; i < ARRAY_SIZE; ++i) + for (size_t i = 0; i < ARRAY_LEN; ++i) ASSERT_EQ(array[i], 100); } @@ -99,12 +96,9 @@ public: int array[25] = {10, 23, 8, 35, 55, 45, 40, 100, 110, 123, 90, 80, 70, 60, 171, 11, 1, -1, -5, -10, 1155, 1170, 1171, 12, -100}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); - - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], -100); ASSERT_EQ(array[1], -10); @@ -135,12 +129,9 @@ public: void test_unsorted_array_2(SortingRoutine sort_func) { int array[7] = {10, 40, 45, 55, 35, 23, 60}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); - - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 10); ASSERT_EQ(array[1], 23); @@ -153,12 +144,9 @@ public: void test_unsorted_array_duplicated_1(SortingRoutine sort_func) { int array[6] = {10, 10, 20, 20, 5, 5}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); - - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 5); ASSERT_EQ(array[1], 5); @@ -170,12 +158,9 @@ public: void test_unsorted_array_duplicated_2(SortingRoutine sort_func) { int array[10] = {20, 10, 10, 10, 10, 20, 21, 21, 21, 21}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); - - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 10); ASSERT_EQ(array[1], 10); @@ -191,12 +176,9 @@ public: void test_unsorted_array_duplicated_3(SortingRoutine sort_func) { int array[10] = {20, 30, 30, 30, 30, 20, 21, 21, 21, 21}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); - - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 20); ASSERT_EQ(array[1], 20); @@ -213,12 +195,9 @@ public: void test_unsorted_three_element_1(SortingRoutine sort_func) { int array[3] = {14999024, 0, 3}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); - - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 0); ASSERT_EQ(array[1], 3); @@ -228,12 +207,9 @@ public: void test_unsorted_three_element_2(SortingRoutine sort_func) { int array[3] = {3, 14999024, 0}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); - - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 0); ASSERT_EQ(array[1], 3); @@ -243,12 +219,9 @@ public: void test_unsorted_three_element_3(SortingRoutine sort_func) { int array[3] = {3, 0, 14999024}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); - - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 0); ASSERT_EQ(array[1], 3); @@ -258,12 +231,9 @@ public: void test_same_three_element(SortingRoutine sort_func) { int array[3] = {12345, 12345, 12345}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); - - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 12345); ASSERT_EQ(array[1], 12345); @@ -273,12 +243,9 @@ public: void test_unsorted_two_element_1(SortingRoutine sort_func) { int array[] = {14999024, 0}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); - - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 0); ASSERT_EQ(array[1], 14999024); @@ -287,12 +254,9 @@ public: void test_unsorted_two_element_2(SortingRoutine sort_func) { int array[] = {0, 14999024}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); - - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 0); ASSERT_EQ(array[1], 14999024); @@ -301,12 +265,9 @@ public: void test_same_two_element(SortingRoutine sort_func) { int array[] = {12345, 12345}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); - - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 12345); ASSERT_EQ(array[1], 12345); @@ -315,15 +276,80 @@ public: void test_single_element(SortingRoutine sort_func) { int array[] = {12345}; - constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int); - - auto arr = Array(reinterpret_cast<uint8_t *>(array), ARRAY_SIZE, - sizeof(int), Comparator(int_compare)); + constexpr size_t ARRAY_LEN = sizeof(array) / sizeof(int); - sort_func(arr); + int_sort(sort_func, array, ARRAY_LEN); ASSERT_EQ(array[0], 12345); } + + void test_different_elem_size(SortingRoutine sort_func) { + // Random order of values [0,50) to avoid only testing pre-sorted handling. + // Long enough to reach interesting code. + constexpr uint8_t ARRAY_INITIAL_VALS[] = { + 42, 13, 8, 4, 17, 28, 20, 32, 22, 29, 7, 2, 46, 37, 26, 49, 24, + 38, 10, 18, 40, 36, 47, 15, 11, 48, 44, 33, 1, 5, 16, 35, 39, 41, + 14, 23, 3, 9, 6, 27, 21, 25, 31, 45, 12, 43, 34, 30, 19, 0}; + + constexpr size_t ARRAY_LEN = sizeof(ARRAY_INITIAL_VALS); + constexpr size_t MAX_ELEM_SIZE = 150; + constexpr size_t BUF_SIZE = ARRAY_LEN * MAX_ELEM_SIZE; + + static_assert(ARRAY_LEN < 256); // so we can encode the values. + + // Minimum alignment to test implementation for bugs related to assuming + // incorrect association between alignment and element size. The buffer is + // 'static' as otherwise it will exhaust the stack on the GPU targets. + alignas(1) static uint8_t buf[BUF_SIZE]; + + // GCC still requires capturing the constant ARRAY_INITIAL_VALS in the + // lambda hence, let's use & to implicitly capture all needed variables + // automatically. + const auto fill_buf = [&](size_t elem_size) { + for (size_t i = 0; i < BUF_SIZE; ++i) { + buf[i] = 0; + } + + for (size_t elem_i = 0, buf_i = 0; elem_i < ARRAY_LEN; ++elem_i) { + const uint8_t elem_val = ARRAY_INITIAL_VALS[elem_i]; + for (size_t elem_byte_i = 0; elem_byte_i < elem_size; ++elem_byte_i) { + buf[buf_i] = elem_val; + buf_i += 1; + } + } + }; + + for (size_t elem_size = 0; elem_size <= MAX_ELEM_SIZE; ++elem_size) { + // Fill all bytes with data to ensure mistakes in elem swap are noticed. + fill_buf(elem_size); + + sort_func(reinterpret_cast<void *>(buf), ARRAY_LEN, elem_size, + [](const void *a, const void *b) -> int { + const uint8_t a_val = *reinterpret_cast<const uint8_t *>(a); + const uint8_t b_val = *reinterpret_cast<const uint8_t *>(b); + + if (a_val < b_val) { + return -1; + } else if (a_val > b_val) { + return 1; + } else { + return 0; + } + }); + + for (size_t elem_i = 0, buf_i = 0; elem_i < ARRAY_LEN; ++elem_i) { + const uint8_t expected_elem_val = static_cast<uint8_t>(elem_i); + + for (size_t elem_byte_i = 0; elem_byte_i < elem_size; ++elem_byte_i) { + const uint8_t buf_val = buf[buf_i]; + // Check that every byte in the element has the expected value. + ASSERT_EQ(buf_val, expected_elem_val) + << "elem_size: " << elem_size << " buf_i: " << buf_i << '\n'; + buf_i += 1; + } + } + } + } }; #define LIST_SORTING_TESTS(Name, Func) \ @@ -374,4 +400,7 @@ public: TEST_F(LlvmLibc##Name##Test, SingleElementArray) { \ test_single_element(Func); \ } \ + TEST_F(LlvmLibc##Name##Test, DifferentElemSizeArray) { \ + test_different_elem_size(Func); \ + } \ static_assert(true) diff --git a/libc/test/src/stdlib/heap_sort_test.cpp b/libc/test/src/stdlib/heap_sort_test.cpp index d70e3dc..18d4244 100644 --- a/libc/test/src/stdlib/heap_sort_test.cpp +++ b/libc/test/src/stdlib/heap_sort_test.cpp @@ -7,10 +7,20 @@ //===----------------------------------------------------------------------===// #include "SortingTest.h" -#include "src/stdlib/heap_sort.h" +#include "src/stdlib/qsort_util.h" -void sort(const LIBC_NAMESPACE::internal::Array &array) { - LIBC_NAMESPACE::internal::heap_sort(array); +void heap_sort(void *array, size_t array_size, size_t elem_size, + int (*compare)(const void *, const void *)) { + + constexpr bool USE_QUICKSORT = false; + + const auto is_less = [compare](const void *a, + const void *b) noexcept -> bool { + return compare(a, b) < 0; + }; + + LIBC_NAMESPACE::internal::unstable_sort_impl<USE_QUICKSORT>( + array, array_size, elem_size, is_less); } -LIST_SORTING_TESTS(HeapSort, sort); +LIST_SORTING_TESTS(HeapSort, heap_sort); diff --git a/libc/test/src/stdlib/qsort_r_test.cpp b/libc/test/src/stdlib/qsort_r_test.cpp index 6893fdc..f189236 100644 --- a/libc/test/src/stdlib/qsort_r_test.cpp +++ b/libc/test/src/stdlib/qsort_r_test.cpp @@ -62,9 +62,9 @@ TEST(LlvmLibcQsortRTest, SortedArray) { ASSERT_LE(array[23], 11100); ASSERT_LE(array[24], 12310); - // This is a sorted list, but there still have to have been at least N + // This is a sorted list, but there still have to have been at least N - 1 // comparisons made. - ASSERT_GE(count, ARRAY_SIZE); + ASSERT_GE(count, ARRAY_SIZE - 1); } TEST(LlvmLibcQsortRTest, ReverseSortedArray) { diff --git a/libc/test/src/stdlib/qsort_test.cpp b/libc/test/src/stdlib/qsort_test.cpp deleted file mode 100644 index 1e921a8..0000000 --- a/libc/test/src/stdlib/qsort_test.cpp +++ /dev/null @@ -1,17 +0,0 @@ -//===-- Unittests for qsort -----------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "SortingTest.h" -#include "src/stdlib/qsort.h" - -void sort(const LIBC_NAMESPACE::internal::Array &array) { - LIBC_NAMESPACE::qsort(reinterpret_cast<void *>(array.get(0)), array.size(), - sizeof(int), SortingTest::int_compare); -} - -LIST_SORTING_TESTS(Qsort, sort); diff --git a/libc/test/src/stdlib/quick_sort_test.cpp b/libc/test/src/stdlib/quick_sort_test.cpp index d6bf77e..2832c85 100644 --- a/libc/test/src/stdlib/quick_sort_test.cpp +++ b/libc/test/src/stdlib/quick_sort_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for quick sort ------------------------------------------===// +//===-- Unittests for qsort -----------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -7,10 +7,19 @@ //===----------------------------------------------------------------------===// #include "SortingTest.h" -#include "src/stdlib/quick_sort.h" +#include "src/stdlib/qsort_util.h" -void sort(const LIBC_NAMESPACE::internal::Array &array) { - LIBC_NAMESPACE::internal::quick_sort(array); +void quick_sort(void *array, size_t array_size, size_t elem_size, + int (*compare)(const void *, const void *)) { + constexpr bool USE_QUICKSORT = true; + + const auto is_less = [compare](const void *a, + const void *b) noexcept -> bool { + return compare(a, b) < 0; + }; + + LIBC_NAMESPACE::internal::unstable_sort_impl<USE_QUICKSORT>( + array, array_size, elem_size, is_less); } -LIST_SORTING_TESTS(QuickSort, sort); +LIST_SORTING_TESTS(Qsort, quick_sort); diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt index da3903f..12add22 100644 --- a/libc/test/src/time/CMakeLists.txt +++ b/libc/test/src/time/CMakeLists.txt @@ -13,6 +13,8 @@ add_libc_unittest( 20 DEPENDS libc.src.time.asctime + libc.hdr.types.struct_tm + libc.src.time.time_constants ) add_libc_unittest( @@ -28,6 +30,8 @@ add_libc_unittest( 20 DEPENDS libc.src.time.asctime_r + libc.hdr.types.struct_tm + libc.src.time.time_constants ) add_libc_unittest( @@ -45,7 +49,8 @@ add_libc_unittest( libc.include.time libc.hdr.types.time_t libc.src.time.ctime - libc.src.time.time_utils + libc.src.time.time_constants + libc.hdr.types.struct_tm ) add_libc_unittest( @@ -63,7 +68,8 @@ add_libc_unittest( libc.include.time libc.hdr.types.time_t libc.src.time.ctime_r - libc.src.time.time_utils + libc.src.time.time_constants + libc.hdr.types.struct_tm ) add_libc_test( @@ -74,6 +80,9 @@ add_libc_test( clock_gettime_test.cpp DEPENDS libc.src.time.clock_gettime + libc.hdr.types.time_t + libc.hdr.types.struct_timespec + libc.hdr.time_macros ) add_libc_test( @@ -94,6 +103,8 @@ add_libc_unittest( difftime_test.cpp DEPENDS libc.src.time.difftime + libc.src.time.time_constants + libc.src.__support.FPUtil.fp_bits ) add_libc_unittest( @@ -105,6 +116,7 @@ add_libc_unittest( DEPENDS libc.include.time libc.src.time.gettimeofday + libc.hdr.types.struct_timeval ) add_libc_unittest( @@ -118,6 +130,8 @@ add_libc_unittest( DEPENDS libc.src.time.gmtime libc.src.__support.CPP.limits + libc.hdr.types.struct_tm + libc.src.time.time_constants ) add_libc_unittest( @@ -130,6 +144,8 @@ add_libc_unittest( TmMatcher.h DEPENDS libc.src.time.gmtime_r + libc.hdr.types.struct_tm + libc.src.time.time_constants ) add_libc_unittest( @@ -146,6 +162,8 @@ add_libc_unittest( DEPENDS libc.src.time.mktime libc.src.__support.CPP.limits + libc.hdr.types.struct_tm + libc.src.time.time_constants ) add_libc_test( @@ -158,6 +176,7 @@ add_libc_test( libc.include.time libc.src.time.nanosleep libc.src.errno.errno + libc.hdr.types.struct_timespec ) add_libc_unittest( @@ -180,6 +199,7 @@ add_libc_test( timespec_get_test.cpp DEPENDS libc.src.time.timespec_get + libc.hdr.types.struct_timespec ) add_libc_test( @@ -192,4 +212,5 @@ add_libc_test( libc.include.time libc.src.time.clock libc.src.errno.errno + libc.hdr.types.clock_t ) diff --git a/libc/test/src/time/TmHelper.h b/libc/test/src/time/TmHelper.h index 5ae2584..1582839 100644 --- a/libc/test/src/time/TmHelper.h +++ b/libc/test/src/time/TmHelper.h @@ -9,12 +9,9 @@ #ifndef LLVM_LIBC_TEST_SRC_TIME_TMHELPER_H #define LLVM_LIBC_TEST_SRC_TIME_TMHELPER_H -#include <time.h> - +#include "hdr/types/struct_tm.h" #include "src/__support/macros/config.h" -#include "src/time/time_utils.h" - -using LIBC_NAMESPACE::time_utils::TimeConstants; +#include "src/time/time_constants.h" namespace LIBC_NAMESPACE_DECL { namespace tmhelper { @@ -30,7 +27,7 @@ static inline void initialize_tm_data(struct tm *tm_data, int year, int month, .tm_mday = mday, .tm_mon = month - 1, // tm_mon starts with 0 for Jan // years since 1900 - .tm_year = year - TimeConstants::TIME_YEAR_BASE, + .tm_year = year - time_constants::TIME_YEAR_BASE, .tm_wday = wday, .tm_yday = yday, .tm_isdst = 0}; diff --git a/libc/test/src/time/TmMatcher.h b/libc/test/src/time/TmMatcher.h index 630956b..d39ee39 100644 --- a/libc/test/src/time/TmMatcher.h +++ b/libc/test/src/time/TmMatcher.h @@ -9,8 +9,7 @@ #ifndef LLVM_LIBC_TEST_SRC_TIME_TM_MATCHER_H #define LLVM_LIBC_TEST_SRC_TIME_TM_MATCHER_H -#include <time.h> - +#include "hdr/types/struct_tm.h" #include "src/__support/macros/config.h" #include "test/UnitTest/Test.h" diff --git a/libc/test/src/time/asctime_r_test.cpp b/libc/test/src/time/asctime_r_test.cpp index f3aadbb..b595cfe 100644 --- a/libc/test/src/time/asctime_r_test.cpp +++ b/libc/test/src/time/asctime_r_test.cpp @@ -8,12 +8,10 @@ #include "src/errno/libc_errno.h" #include "src/time/asctime_r.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/Test.h" #include "test/src/time/TmHelper.h" -using LIBC_NAMESPACE::time_utils::TimeConstants; - static inline char *call_asctime_r(struct tm *tm_data, int year, int month, int mday, int hour, int min, int sec, int wday, int yday, char *buffer) { @@ -30,7 +28,7 @@ TEST(LlvmLibcAsctimeR, Nullptr) { ASSERT_ERRNO_EQ(EINVAL); ASSERT_STREQ(nullptr, result); - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; result = LIBC_NAMESPACE::asctime_r(nullptr, buffer); ASSERT_ERRNO_EQ(EINVAL); ASSERT_STREQ(nullptr, result); @@ -42,7 +40,7 @@ TEST(LlvmLibcAsctimeR, Nullptr) { } TEST(LlvmLibcAsctimeR, ValidDate) { - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; struct tm tm_data; char *result; // 1970-01-01 00:00:00. Test with a valid buffer size. diff --git a/libc/test/src/time/clock_gettime_test.cpp b/libc/test/src/time/clock_gettime_test.cpp index 43715c0..d3edcae 100644 --- a/libc/test/src/time/clock_gettime_test.cpp +++ b/libc/test/src/time/clock_gettime_test.cpp @@ -6,12 +6,13 @@ // //===----------------------------------------------------------------------===// +#include "hdr/time_macros.h" +#include "hdr/types/struct_timespec.h" +#include "hdr/types/time_t.h" #include "src/__support/macros/properties/architectures.h" #include "src/time/clock_gettime.h" #include "test/UnitTest/Test.h" -#include <time.h> - TEST(LlvmLibcClockGetTime, RealTime) { timespec tp; int result; diff --git a/libc/test/src/time/clock_test.cpp b/libc/test/src/time/clock_test.cpp index 05082aa..8d8d89d 100644 --- a/libc/test/src/time/clock_test.cpp +++ b/libc/test/src/time/clock_test.cpp @@ -6,11 +6,10 @@ // //===----------------------------------------------------------------------===// +#include "hdr/types/clock_t.h" #include "src/time/clock.h" #include "test/UnitTest/Test.h" -#include <time.h> - TEST(LlvmLibcClockTest, SmokeTest) { clock_t c1 = LIBC_NAMESPACE::clock(); ASSERT_GT(c1, clock_t(0)); diff --git a/libc/test/src/time/ctime_r_test.cpp b/libc/test/src/time/ctime_r_test.cpp index 9ce6f75..27011b7 100644 --- a/libc/test/src/time/ctime_r_test.cpp +++ b/libc/test/src/time/ctime_r_test.cpp @@ -8,18 +8,16 @@ #include "src/errno/libc_errno.h" #include "src/time/ctime_r.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/Test.h" #include "test/src/time/TmHelper.h" -using LIBC_NAMESPACE::time_utils::TimeConstants; - TEST(LlvmLibcCtimeR, Nullptr) { char *result; result = LIBC_NAMESPACE::ctime_r(nullptr, nullptr); ASSERT_STREQ(nullptr, result); - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; result = LIBC_NAMESPACE::ctime_r(nullptr, buffer); ASSERT_STREQ(nullptr, result); @@ -29,7 +27,7 @@ TEST(LlvmLibcCtimeR, Nullptr) { } TEST(LlvmLibcCtimeR, ValidUnixTimestamp0) { - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; time_t t; char *result; // 1970-01-01 00:00:00. Test with a valid buffer size. @@ -39,7 +37,7 @@ TEST(LlvmLibcCtimeR, ValidUnixTimestamp0) { } TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) { - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; time_t t; char *result; // 2038-01-19 03:14:07. Test with a valid buffer size. @@ -49,7 +47,7 @@ TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) { } TEST(LlvmLibcCtimeR, InvalidArgument) { - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; time_t t; char *result; t = 2147483648; diff --git a/libc/test/src/time/difftime_test.cpp b/libc/test/src/time/difftime_test.cpp index 68ff463..4dab1ac 100644 --- a/libc/test/src/time/difftime_test.cpp +++ b/libc/test/src/time/difftime_test.cpp @@ -8,15 +8,12 @@ #include "src/__support/FPUtil/FPBits.h" #include "src/time/difftime.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/ErrnoSetterMatcher.h" #include "test/UnitTest/Test.h" -using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; -using LIBC_NAMESPACE::time_utils::TimeConstants; - TEST(LlvmLibcDifftime, SmokeTest) { - time_t t1_seconds = TimeConstants::SECONDS_PER_HOUR; + time_t t1_seconds = LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR; time_t t2_seconds = 0; LIBC_NAMESPACE::fputil::FPBits<long double> expected_fp = diff --git a/libc/test/src/time/gettimeofday_test.cpp b/libc/test/src/time/gettimeofday_test.cpp index ee934b7..8f9f136 100644 --- a/libc/test/src/time/gettimeofday_test.cpp +++ b/libc/test/src/time/gettimeofday_test.cpp @@ -6,8 +6,7 @@ // //===----------------------------------------------------------------------===// -#include <time.h> - +#include "hdr/types/struct_timeval.h" #include "src/time/gettimeofday.h" #include "test/UnitTest/Test.h" diff --git a/libc/test/src/time/gmtime_r_test.cpp b/libc/test/src/time/gmtime_r_test.cpp index 2276b48..9d466f4 100644 --- a/libc/test/src/time/gmtime_r_test.cpp +++ b/libc/test/src/time/gmtime_r_test.cpp @@ -7,12 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/time/gmtime_r.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/Test.h" #include "test/src/time/TmMatcher.h" -using LIBC_NAMESPACE::time_utils::TimeConstants; - // gmtime and gmtime_r share the same code and thus didn't repeat all the tests // from gmtime. Added couple of validation tests. TEST(LlvmLibcGmTimeR, EndOf32BitEpochYear) { @@ -22,16 +20,17 @@ TEST(LlvmLibcGmTimeR, EndOf32BitEpochYear) { struct tm tm_data; struct tm *tm_data_ptr; tm_data_ptr = LIBC_NAMESPACE::gmtime_r(&seconds, &tm_data); - EXPECT_TM_EQ((tm{7, // sec - 14, // min - 3, // hr - 19, // day - 0, // tm_mon starts with 0 for Jan - 2038 - TimeConstants::TIME_YEAR_BASE, // year - 2, // wday - 7, // yday - 0}), - *tm_data_ptr); + EXPECT_TM_EQ( + (tm{7, // sec + 14, // min + 3, // hr + 19, // day + 0, // tm_mon starts with 0 for Jan + 2038 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 2, // wday + 7, // yday + 0}), + *tm_data_ptr); EXPECT_TM_EQ(*tm_data_ptr, tm_data); } @@ -43,15 +42,16 @@ TEST(LlvmLibcGmTimeR, Max64BitYear) { struct tm tm_data; struct tm *tm_data_ptr; tm_data_ptr = LIBC_NAMESPACE::gmtime_r(&seconds, &tm_data); - EXPECT_TM_EQ((tm{50, // sec - 50, // min - 12, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 2147483647 - TimeConstants::TIME_YEAR_BASE, // year - 2, // wday - 50, // yday - 0}), - *tm_data_ptr); + EXPECT_TM_EQ( + (tm{50, // sec + 50, // min + 12, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 2147483647 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 2, // wday + 50, // yday + 0}), + *tm_data_ptr); EXPECT_TM_EQ(*tm_data_ptr, tm_data); } diff --git a/libc/test/src/time/gmtime_test.cpp b/libc/test/src/time/gmtime_test.cpp index 433fbf6..6af5a18 100644 --- a/libc/test/src/time/gmtime_test.cpp +++ b/libc/test/src/time/gmtime_test.cpp @@ -6,32 +6,36 @@ // //===----------------------------------------------------------------------===// +#include "hdr/types/struct_tm.h" #include "src/__support/CPP/limits.h" // INT_MAX, INT_MIN #include "src/errno/libc_errno.h" #include "src/time/gmtime.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/ErrnoSetterMatcher.h" #include "test/UnitTest/Test.h" #include "test/src/time/TmMatcher.h" using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; -using LIBC_NAMESPACE::time_utils::TimeConstants; TEST(LlvmLibcGmTime, OutOfRange) { if (sizeof(time_t) < sizeof(int64_t)) return; time_t seconds = - 1 + INT_MAX * static_cast<int64_t>( - TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR); + 1 + + INT_MAX * + static_cast<int64_t>( + LIBC_NAMESPACE::time_constants::NUMBER_OF_SECONDS_IN_LEAP_YEAR); struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds); EXPECT_TRUE(tm_data == nullptr); ASSERT_ERRNO_EQ(EOVERFLOW); LIBC_NAMESPACE::libc_errno = 0; - seconds = INT_MIN * static_cast<int64_t>( - TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR) - - 1; + seconds = + INT_MIN * + static_cast<int64_t>( + LIBC_NAMESPACE::time_constants::NUMBER_OF_SECONDS_IN_LEAP_YEAR) - + 1; tm_data = LIBC_NAMESPACE::gmtime(&seconds); EXPECT_TRUE(tm_data == nullptr); ASSERT_ERRNO_EQ(EOVERFLOW); @@ -43,201 +47,215 @@ TEST(LlvmLibcGmTime, InvalidSeconds) { // -1 second from 1970-01-01 00:00:00 returns 1969-12-31 23:59:59. seconds = -1; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{59, // sec - 59, // min - 23, // hr - 31, // day - 12 - 1, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 364, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{59, // sec + 59, // min + 23, // hr + 31, // day + 12 - 1, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 364, // yday + 0}), + *tm_data); // 60 seconds from 1970-01-01 00:00:00 returns 1970-01-01 00:01:00. seconds = 60; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 1, // min - 0, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 1970 - TimeConstants::TIME_YEAR_BASE, // year - 4, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 1, // min + 0, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 4, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, InvalidMinutes) { time_t seconds = 0; struct tm *tm_data = nullptr; // -1 minute from 1970-01-01 00:00:00 returns 1969-12-31 23:59:00. - seconds = -TimeConstants::SECONDS_PER_MIN; + seconds = -LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 59, // min - 23, // hr - 31, // day - 11, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 59, // min + 23, // hr + 31, // day + 11, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 0, // yday + 0}), + *tm_data); // 60 minutes from 1970-01-01 00:00:00 returns 1970-01-01 01:00:00. - seconds = 60 * TimeConstants::SECONDS_PER_MIN; + seconds = 60 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 1, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 1970 - TimeConstants::TIME_YEAR_BASE, // year - 4, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 1, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 4, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, InvalidHours) { time_t seconds = 0; struct tm *tm_data = nullptr; // -1 hour from 1970-01-01 00:00:00 returns 1969-12-31 23:00:00. - seconds = -TimeConstants::SECONDS_PER_HOUR; + seconds = -LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 23, // hr - 31, // day - 11, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 23, // hr + 31, // day + 11, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 0, // yday + 0}), + *tm_data); // 24 hours from 1970-01-01 00:00:00 returns 1970-01-02 00:00:00. - seconds = 24 * TimeConstants::SECONDS_PER_HOUR; + seconds = 24 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 2, // day - 0, // tm_mon starts with 0 for Jan - 1970 - TimeConstants::TIME_YEAR_BASE, // year - 5, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 2, // day + 0, // tm_mon starts with 0 for Jan + 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 5, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, InvalidYear) { // -1 year from 1970-01-01 00:00:00 returns 1969-01-01 00:00:00. - time_t seconds = - -TimeConstants::DAYS_PER_NON_LEAP_YEAR * TimeConstants::SECONDS_PER_DAY; + time_t seconds = -LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, InvalidMonths) { time_t seconds = 0; struct tm *tm_data = nullptr; // -1 month from 1970-01-01 00:00:00 returns 1969-12-01 00:00:00. - seconds = -31 * TimeConstants::SECONDS_PER_DAY; + seconds = -31 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 12 - 1, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 1, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 12 - 1, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 1, // wday + 0, // yday + 0}), + *tm_data); // 1970-13-01 00:00:00 returns 1971-01-01 00:00:00. - seconds = - TimeConstants::DAYS_PER_NON_LEAP_YEAR * TimeConstants::SECONDS_PER_DAY; + seconds = LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 1971 - TimeConstants::TIME_YEAR_BASE, // year - 5, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 1971 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 5, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, InvalidDays) { time_t seconds = 0; struct tm *tm_data = nullptr; // -1 day from 1970-01-01 00:00:00 returns 1969-12-31 00:00:00. - seconds = -1 * TimeConstants::SECONDS_PER_DAY; + seconds = -1 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 31, // day - 11, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 31, // day + 11, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 0, // yday + 0}), + *tm_data); // 1970-01-32 00:00:00 returns 1970-02-01 00:00:00. - seconds = 31 * TimeConstants::SECONDS_PER_DAY; + seconds = 31 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 1970 - TimeConstants::TIME_YEAR_BASE, // year - 0, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 0, // wday + 0, // yday + 0}), + *tm_data); // 1970-02-29 00:00:00 returns 1970-03-01 00:00:00. - seconds = 59 * TimeConstants::SECONDS_PER_DAY; + seconds = 59 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 2, // tm_mon starts with 0 for Jan - 1970 - TimeConstants::TIME_YEAR_BASE, // year - 0, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 2, // tm_mon starts with 0 for Jan + 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 0, // wday + 0, // yday + 0}), + *tm_data); // 1972-02-30 00:00:00 returns 1972-03-01 00:00:00. - seconds = ((2 * TimeConstants::DAYS_PER_NON_LEAP_YEAR) + 60) * - TimeConstants::SECONDS_PER_DAY; + seconds = + ((2 * LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR) + 60) * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 2, // tm_mon starts with 0 for Jan - 1972 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 2, // tm_mon starts with 0 for Jan + 1972 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, EndOf32BitEpochYear) { @@ -245,16 +263,17 @@ TEST(LlvmLibcGmTime, EndOf32BitEpochYear) { // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC. time_t seconds = 0x7FFFFFFF; struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{7, // sec - 14, // min - 3, // hr - 19, // day - 0, // tm_mon starts with 0 for Jan - 2038 - TimeConstants::TIME_YEAR_BASE, // year - 2, // wday - 7, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{7, // sec + 14, // min + 3, // hr + 19, // day + 0, // tm_mon starts with 0 for Jan + 2038 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 2, // wday + 7, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, Max64BitYear) { @@ -263,28 +282,30 @@ TEST(LlvmLibcGmTime, Max64BitYear) { // Mon Jan 1 12:50:50 2170 (200 years from 1970), time_t seconds = 6311479850; struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{50, // sec - 50, // min - 12, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 2170 - TimeConstants::TIME_YEAR_BASE, // year - 1, // wday - 50, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{50, // sec + 50, // min + 12, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 2170 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 1, // wday + 50, // yday + 0}), + *tm_data); // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year. seconds = 67767976202043050; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{50, // sec - 50, // min - 12, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 2147483647 - TimeConstants::TIME_YEAR_BASE, // year - 2, // wday - 50, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{50, // sec + 50, // min + 12, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 2147483647 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 2, // wday + 50, // yday + 0}), + *tm_data); } diff --git a/libc/test/src/time/mktime_test.cpp b/libc/test/src/time/mktime_test.cpp index 84e6c7e..fe1116f 100644 --- a/libc/test/src/time/mktime_test.cpp +++ b/libc/test/src/time/mktime_test.cpp @@ -8,7 +8,7 @@ #include "src/__support/CPP/limits.h" // INT_MAX #include "src/time/mktime.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/ErrnoSetterMatcher.h" #include "test/UnitTest/Test.h" #include "test/src/time/TmHelper.h" @@ -16,29 +16,37 @@ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; -using LIBC_NAMESPACE::time_utils::Month; +using LIBC_NAMESPACE::time_constants::Month; static inline constexpr int tm_year(int year) { - return year - TimeConstants::TIME_YEAR_BASE; + return year - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE; } TEST(LlvmLibcMkTime, FailureSetsErrno) { - struct tm tm_data { - .tm_sec = INT_MAX, .tm_min = INT_MAX, .tm_hour = INT_MAX, - .tm_mday = INT_MAX, .tm_mon = INT_MAX - 1, .tm_year = tm_year(INT_MAX), - .tm_wday = 0, .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = INT_MAX, + .tm_min = INT_MAX, + .tm_hour = INT_MAX, + .tm_mday = INT_MAX, + .tm_mon = INT_MAX - 1, + .tm_year = tm_year(INT_MAX), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } TEST(LlvmLibcMkTime, InvalidSeconds) { { // -1 second from 1970-01-01 00:00:00 returns 1969-12-31 23:59:59. - struct tm tm_data { - .tm_sec = -1, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = -1, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(-1)); EXPECT_TM_EQ((tm{.tm_sec = 59, .tm_min = 59, @@ -54,11 +62,15 @@ TEST(LlvmLibcMkTime, InvalidSeconds) { { // 60 seconds from 1970-01-01 00:00:00 returns 1970-01-01 00:01:00. - struct tm tm_data { - .tm_sec = 60, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 60, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(60)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 1, @@ -76,13 +88,17 @@ TEST(LlvmLibcMkTime, InvalidSeconds) { TEST(LlvmLibcMkTime, InvalidMinutes) { { // -1 minute from 1970-01-01 00:00:00 returns 1969-12-31 23:59:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = -1, .tm_hour = 0, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = -1, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(-TimeConstants::SECONDS_PER_MIN)); + Succeeds(-LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 59, .tm_hour = 23, @@ -97,13 +113,17 @@ TEST(LlvmLibcMkTime, InvalidMinutes) { { // 60 minutes from 1970-01-01 00:00:00 returns 1970-01-01 01:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 60, .tm_hour = 0, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 60, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(60 * TimeConstants::SECONDS_PER_MIN)); + Succeeds(60 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 1, @@ -120,13 +140,17 @@ TEST(LlvmLibcMkTime, InvalidMinutes) { TEST(LlvmLibcMkTime, InvalidHours) { { // -1 hour from 1970-01-01 00:00:00 returns 1969-12-31 23:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = -1, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = -1, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(-TimeConstants::SECONDS_PER_HOUR)); + Succeeds(-LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 23, @@ -141,13 +165,18 @@ TEST(LlvmLibcMkTime, InvalidHours) { { // 24 hours from 1970-01-01 00:00:00 returns 1970-01-02 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 24, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; - EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(24 * TimeConstants::SECONDS_PER_HOUR)); + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 24, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; + EXPECT_THAT( + LIBC_NAMESPACE::mktime(&tm_data), + Succeeds(24 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -163,14 +192,18 @@ TEST(LlvmLibcMkTime, InvalidHours) { TEST(LlvmLibcMkTime, InvalidYear) { // -1 year from 1970-01-01 00:00:00 returns 1969-01-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1969), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1969), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(-TimeConstants::DAYS_PER_NON_LEAP_YEAR * - TimeConstants::SECONDS_PER_DAY)); + Succeeds(-LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -188,61 +221,85 @@ TEST(LlvmLibcMkTime, InvalidEndOf32BitEpochYear) { return; { // 2038-01-19 03:14:08 tests overflow of the second in 2038. - struct tm tm_data { - .tm_sec = 8, .tm_min = 14, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 8, + .tm_min = 14, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } { // 2038-01-19 03:15:07 tests overflow of the minute in 2038. - struct tm tm_data { - .tm_sec = 7, .tm_min = 15, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 15, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } { // 2038-01-19 04:14:07 tests overflow of the hour in 2038. - struct tm tm_data { - .tm_sec = 7, .tm_min = 14, .tm_hour = 4, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 14, + .tm_hour = 4, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } { // 2038-01-20 03:14:07 tests overflow of the day in 2038. - struct tm tm_data { - .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 20, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 14, + .tm_hour = 3, + .tm_mday = 20, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } { // 2038-02-19 03:14:07 tests overflow of the month in 2038. - struct tm tm_data { - .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::FEBRUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 14, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::FEBRUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } { // 2039-01-19 03:14:07 tests overflow of the year. - struct tm tm_data { - .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2039), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 14, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2039), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } } @@ -250,12 +307,18 @@ TEST(LlvmLibcMkTime, InvalidEndOf32BitEpochYear) { TEST(LlvmLibcMkTime, InvalidMonths) { { // -1 month from 1970-01-01 00:00:00 returns 1969-12-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 0, .tm_mon = -1, - .tm_year = tm_year(1970), .tm_wday = 0, .tm_yday = 0, .tm_isdst = 0 - }; - EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(-32 * TimeConstants::SECONDS_PER_DAY)); + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 0, + .tm_mon = -1, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; + EXPECT_THAT( + LIBC_NAMESPACE::mktime(&tm_data), + Succeeds(-32 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -270,13 +333,19 @@ TEST(LlvmLibcMkTime, InvalidMonths) { { // 1970-13-01 00:00:00 returns 1971-01-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, .tm_mon = 12, - .tm_year = tm_year(1970), .tm_wday = 0, .tm_yday = 0, .tm_isdst = 0 - }; - EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(TimeConstants::DAYS_PER_NON_LEAP_YEAR * - TimeConstants::SECONDS_PER_DAY)); + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = 12, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; + EXPECT_THAT( + LIBC_NAMESPACE::mktime(&tm_data), + Succeeds(LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -293,13 +362,17 @@ TEST(LlvmLibcMkTime, InvalidMonths) { TEST(LlvmLibcMkTime, InvalidDays) { { // -1 day from 1970-01-01 00:00:00 returns 1969-12-31 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = (1 - 1), - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = (1 - 1), + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(-1 * TimeConstants::SECONDS_PER_DAY)); + Succeeds(-1 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -314,13 +387,17 @@ TEST(LlvmLibcMkTime, InvalidDays) { { // 1970-01-32 00:00:00 returns 1970-02-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 32, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 32, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(31 * TimeConstants::SECONDS_PER_DAY)); + Succeeds(31 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -335,13 +412,17 @@ TEST(LlvmLibcMkTime, InvalidDays) { { // 1970-02-29 00:00:00 returns 1970-03-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 29, - .tm_mon = Month::FEBRUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 29, + .tm_mon = Month::FEBRUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(59 * TimeConstants::SECONDS_PER_DAY)); + Succeeds(59 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -356,14 +437,20 @@ TEST(LlvmLibcMkTime, InvalidDays) { { // 1972-02-30 00:00:00 returns 1972-03-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 30, - .tm_mon = Month::FEBRUARY, .tm_year = tm_year(1972), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; - EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(((2 * TimeConstants::DAYS_PER_NON_LEAP_YEAR) + 60) * - TimeConstants::SECONDS_PER_DAY)); + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 30, + .tm_mon = Month::FEBRUARY, + .tm_year = tm_year(1972), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; + EXPECT_THAT( + LIBC_NAMESPACE::mktime(&tm_data), + Succeeds(((2 * LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR) + + 60) * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -381,11 +468,15 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) { // Test for maximum value of a signed 32-bit integer. // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC. { - struct tm tm_data { - .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 14, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(0x7FFFFFFF)); EXPECT_TM_EQ((tm{.tm_sec = 7, .tm_min = 14, @@ -403,11 +494,15 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) { { // 2038-01-19 03:13:59 tests that even a large seconds field is // accepted if the minutes field is smaller. - struct tm tm_data { - .tm_sec = 59, .tm_min = 13, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 59, + .tm_min = 13, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(0x7FFFFFFF - 8)); EXPECT_TM_EQ((tm{.tm_sec = 59, .tm_min = 13, @@ -424,13 +519,18 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) { { // 2038-01-19 02:59:59 tests that large seconds and minutes are // accepted if the hours field is smaller. - struct tm tm_data { - .tm_sec = 59, .tm_min = 59, .tm_hour = 2, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 59, + .tm_min = 59, + .tm_hour = 2, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(0x7FFFFFFF - 8 - 14 * TimeConstants::SECONDS_PER_MIN)); + Succeeds(0x7FFFFFFF - 8 - + 14 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN)); EXPECT_TM_EQ((tm{.tm_sec = 59, .tm_min = 59, .tm_hour = 2, @@ -446,14 +546,19 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) { { // 2038-01-18 23:59:59 tests that large seconds, minutes and hours // are accepted if the days field is smaller. - struct tm tm_data { - .tm_sec = 59, .tm_min = 59, .tm_hour = 23, .tm_mday = 18, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 59, + .tm_min = 59, + .tm_hour = 23, + .tm_mday = 18, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(0x7FFFFFFF - 8 - 14 * TimeConstants::SECONDS_PER_MIN - - 3 * TimeConstants::SECONDS_PER_HOUR)); + Succeeds(0x7FFFFFFF - 8 - + 14 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN - + 3 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR)); EXPECT_TM_EQ((tm{.tm_sec = 59, .tm_min = 59, .tm_hour = 23, @@ -469,15 +574,20 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) { { // 2038-01-18 23:59:59 tests that the final second of 2037 is // accepted. - struct tm tm_data { - .tm_sec = 59, .tm_min = 59, .tm_hour = 23, .tm_mday = 31, - .tm_mon = Month::DECEMBER, .tm_year = tm_year(2037), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 59, + .tm_min = 59, + .tm_hour = 23, + .tm_mday = 31, + .tm_mon = Month::DECEMBER, + .tm_year = tm_year(2037), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(0x7FFFFFFF - 8 - 14 * TimeConstants::SECONDS_PER_MIN - - 3 * TimeConstants::SECONDS_PER_HOUR - - 18 * TimeConstants::SECONDS_PER_DAY)); + Succeeds(0x7FFFFFFF - 8 - + 14 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN - + 3 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR - + 18 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 59, .tm_min = 59, .tm_hour = 23, @@ -496,11 +606,15 @@ TEST(LlvmLibcMkTime, Max64BitYear) { return; { // Mon Jan 1 12:50:50 2170 (200 years from 1970), - struct tm tm_data { - .tm_sec = 50, .tm_min = 50, .tm_hour = 12, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2170), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 50, + .tm_min = 50, + .tm_hour = 12, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2170), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(6311479850)); EXPECT_TM_EQ((tm{.tm_sec = 50, .tm_min = 50, @@ -516,11 +630,15 @@ TEST(LlvmLibcMkTime, Max64BitYear) { { // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year. - struct tm tm_data { - .tm_sec = 50, .tm_min = 50, .tm_hour = 12, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2147483647), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 50, + .tm_min = 50, + .tm_hour = 12, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2147483647), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(67767976202043050)); EXPECT_TM_EQ((tm{.tm_sec = 50, .tm_min = 50, diff --git a/libc/test/src/time/nanosleep_test.cpp b/libc/test/src/time/nanosleep_test.cpp index 2a6eea4..d4f98e2 100644 --- a/libc/test/src/time/nanosleep_test.cpp +++ b/libc/test/src/time/nanosleep_test.cpp @@ -6,8 +6,7 @@ // //===----------------------------------------------------------------------===// -#include <time.h> - +#include "hdr/types/struct_timespec.h" #include "src/errno/libc_errno.h" #include "src/time/nanosleep.h" #include "test/UnitTest/ErrnoSetterMatcher.h" |