mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 23:08:31 +00:00
Introduce more CTL content
This change introduces accumulate, addressof, advance, all_of, distance, array, enable_if, allocator_traits, back_inserter, bad_alloc, is_signed, any_of, copy, exception, fill, fill_n, is_same, is_same_v, out_of_range, lexicographical_compare, is_integral, uninitialized_fill_n, is_unsigned, numeric_limits, uninitialized_fill, iterator_traits, move_backward, min, max, iterator_tag, move_iterator, reverse_iterator, uninitialized_move_n This change experiments with rewriting the ctl::vector class to make the CTL design more similar to the STL. So far it has not slowed things down to have 42 #include lines rather than 2, since it's still almost nothing compared to LLVM's code. In fact the closer we can flirt with being just like libcxx, the better chance we might have of discovering exactly what makes it so slow to compile. It would be an enormous discovery if we can find one simple trick to solving the issue there instead. This also fixes a bug in `ctl::string(const string &s)` when `s` is big.
This commit is contained in:
parent
054da021d0
commit
38921dc46b
52 changed files with 2980 additions and 193 deletions
|
@ -17,6 +17,10 @@ TEST_CTL_DIRECTDEPS = \
|
|||
LIBC_STDIO \
|
||||
LIBC_INTRIN \
|
||||
LIBC_MEM \
|
||||
LIBC_STDIO \
|
||||
THIRD_PARTY_LIBCXX \
|
||||
THIRD_PARTY_LIBCXXABI \
|
||||
THIRD_PARTY_LIBUNWIND \
|
||||
|
||||
TEST_CTL_DEPS := \
|
||||
$(call uniq,$(foreach x,$(TEST_CTL_DIRECTDEPS),$($(x))))
|
||||
|
@ -33,6 +37,10 @@ o/$(MODE)/test/ctl/%.dbg: \
|
|||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
$(TEST_CTL_OBJS): private \
|
||||
OVERRIDE_CXXFLAGS += \
|
||||
-fexceptions \
|
||||
|
||||
.PHONY: o/$(MODE)/test/ctl
|
||||
o/$(MODE)/test/ctl: \
|
||||
$(TEST_CTL_BINS) \
|
||||
|
|
51
test/ctl/accumulate_test.cc
Normal file
51
test/ctl/accumulate_test.cc
Normal file
|
@ -0,0 +1,51 @@
|
|||
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
|
||||
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
||||
//
|
||||
// Copyright 2024 Justine Alexandra Roberts Tunney
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for
|
||||
// any purpose with or without fee is hereby granted, provided that the
|
||||
// above copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include "ctl/accumulate.h"
|
||||
#include "ctl/array.h"
|
||||
|
||||
// #include <array>
|
||||
// #include <numeric>
|
||||
// #define ctl std
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
ctl::array<int, 5> arr = { 1, 2, 3, 4, 5 };
|
||||
|
||||
// Test basic accumulation with addition
|
||||
if (ctl::accumulate(arr.begin(), arr.end(), 0) != 15)
|
||||
return 1;
|
||||
|
||||
// Test accumulation with initial value
|
||||
if (ctl::accumulate(arr.begin(), arr.end(), 10) != 25)
|
||||
return 2;
|
||||
|
||||
// Test accumulation with custom operation (multiplication)
|
||||
if (ctl::accumulate(
|
||||
arr.begin(), arr.end(), 1, [](int a, int b) { return a * b; }) != 120)
|
||||
return 3;
|
||||
|
||||
// Test accumulation with empty range
|
||||
if (ctl::accumulate(arr.end(), arr.end(), 0) != 0)
|
||||
return 4;
|
||||
|
||||
// Test accumulation with single element
|
||||
if (ctl::accumulate(arr.begin(), arr.begin() + 1, 0) != 1)
|
||||
return 5;
|
||||
}
|
56
test/ctl/advance_test.cc
Normal file
56
test/ctl/advance_test.cc
Normal file
|
@ -0,0 +1,56 @@
|
|||
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
|
||||
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
||||
//
|
||||
// Copyright 2024 Justine Alexandra Roberts Tunney
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for
|
||||
// any purpose with or without fee is hereby granted, provided that the
|
||||
// above copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include "ctl/advance.h"
|
||||
#include "ctl/array.h"
|
||||
|
||||
// #include <array>
|
||||
// #include <iterator>
|
||||
// #define ctl std
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
ctl::array<int, 5> arr = { 1, 2, 3, 4, 5 };
|
||||
|
||||
// Test advancing forward
|
||||
auto it = arr.begin();
|
||||
ctl::advance(it, 2);
|
||||
if (*it != 3)
|
||||
return 1;
|
||||
|
||||
// Test advancing to the end
|
||||
ctl::advance(it, 2);
|
||||
if (it != arr.end() - 1)
|
||||
return 2;
|
||||
|
||||
// Test advancing backward
|
||||
ctl::advance(it, -2);
|
||||
if (*it != 3)
|
||||
return 3;
|
||||
|
||||
// Test advancing by zero
|
||||
ctl::advance(it, 0);
|
||||
if (*it != 3)
|
||||
return 4;
|
||||
|
||||
// Test advancing to the beginning
|
||||
ctl::advance(it, -2);
|
||||
if (it != arr.begin())
|
||||
return 5;
|
||||
}
|
52
test/ctl/all_of_test.cc
Normal file
52
test/ctl/all_of_test.cc
Normal file
|
@ -0,0 +1,52 @@
|
|||
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
|
||||
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
||||
//
|
||||
// Copyright 2024 Justine Alexandra Roberts Tunney
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for
|
||||
// any purpose with or without fee is hereby granted, provided that the
|
||||
// above copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include "ctl/all_of.h"
|
||||
#include "ctl/array.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#define ctl std
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
ctl::array<int, 5> arr1 = { 2, 4, 6, 8, 10 };
|
||||
ctl::array<int, 5> arr2 = { 2, 4, 5, 8, 10 };
|
||||
|
||||
// Test when all elements satisfy the condition
|
||||
if (!ctl::all_of(
|
||||
arr1.begin(), arr1.end(), [](int n) { return n % 2 == 0; }))
|
||||
return 1;
|
||||
|
||||
// Test when not all elements satisfy the condition
|
||||
if (ctl::all_of(arr2.begin(), arr2.end(), [](int n) { return n % 2 == 0; }))
|
||||
return 2;
|
||||
|
||||
// Test with empty range
|
||||
if (!ctl::all_of(arr1.end(), arr1.end(), [](int n) { return false; }))
|
||||
return 3;
|
||||
|
||||
// Test with all elements satisfying a different condition
|
||||
if (!ctl::all_of(arr1.begin(), arr1.end(), [](int n) { return n > 0; }))
|
||||
return 4;
|
||||
|
||||
// Test with no elements satisfying the condition
|
||||
if (ctl::all_of(arr1.begin(), arr1.end(), [](int n) { return n > 10; }))
|
||||
return 5;
|
||||
}
|
51
test/ctl/any_of_test.cc
Normal file
51
test/ctl/any_of_test.cc
Normal file
|
@ -0,0 +1,51 @@
|
|||
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
|
||||
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
||||
//
|
||||
// Copyright 2024 Justine Alexandra Roberts Tunney
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for
|
||||
// any purpose with or without fee is hereby granted, provided that the
|
||||
// above copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include "ctl/any_of.h"
|
||||
#include "ctl/array.h"
|
||||
|
||||
// #include <algorithm>
|
||||
// #include <array>
|
||||
// #define ctl std
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
ctl::array<int, 5> arr1 = { 1, 3, 5, 7, 9 };
|
||||
ctl::array<int, 5> arr2 = { 2, 4, 6, 8, 10 };
|
||||
|
||||
// Test when at least one element satisfies the condition
|
||||
if (!ctl::any_of(arr1.begin(), arr1.end(), [](int n) { return n == 7; }))
|
||||
return 1;
|
||||
|
||||
// Test when no elements satisfy the condition
|
||||
if (ctl::any_of(arr1.begin(), arr1.end(), [](int n) { return n == 11; }))
|
||||
return 2;
|
||||
|
||||
// Test with empty range
|
||||
if (ctl::any_of(arr1.end(), arr1.end(), [](int n) { return true; }))
|
||||
return 3;
|
||||
|
||||
// Test when all elements satisfy the condition
|
||||
if (!ctl::any_of(arr2.begin(), arr2.end(), [](int n) { return true; }))
|
||||
return 4;
|
||||
|
||||
// Test with a different condition
|
||||
if (!ctl::any_of(arr1.begin(), arr1.end(), [](int n) { return n > 5; }))
|
||||
return 5;
|
||||
}
|
267
test/ctl/array_test.cc
Normal file
267
test/ctl/array_test.cc
Normal file
|
@ -0,0 +1,267 @@
|
|||
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
|
||||
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
||||
//
|
||||
// Copyright 2024 Justine Alexandra Roberts Tunney
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for
|
||||
// any purpose with or without fee is hereby granted, provided that the
|
||||
// above copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include "ctl/array.h"
|
||||
|
||||
// #include <array>
|
||||
// #define ctl std
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
|
||||
// Test construction and basic properties
|
||||
{
|
||||
ctl::array<int, 5> arr = { 1, 2, 3, 4, 5 };
|
||||
if (arr.size() != 5)
|
||||
return 2;
|
||||
if (arr.max_size() != 5)
|
||||
return 3;
|
||||
if (arr.empty())
|
||||
return 4;
|
||||
}
|
||||
|
||||
// Test element access
|
||||
{
|
||||
ctl::array<int, 3> arr = { 10, 20, 30 };
|
||||
if (arr[0] != 10 || arr[1] != 20 || arr[2] != 30)
|
||||
return 5;
|
||||
if (arr.front() != 10)
|
||||
return 6;
|
||||
if (arr.back() != 30)
|
||||
return 7;
|
||||
}
|
||||
|
||||
// Test data() method
|
||||
{
|
||||
ctl::array<int, 3> arr = { 1, 2, 3 };
|
||||
int* data = arr.data();
|
||||
if (data[0] != 1 || data[1] != 2 || data[2] != 3)
|
||||
return 9;
|
||||
}
|
||||
|
||||
// Test iterators
|
||||
{
|
||||
ctl::array<int, 3> arr = { 1, 2, 3 };
|
||||
int sum = 0;
|
||||
for (auto it = arr.begin(); it != arr.end(); ++it) {
|
||||
sum += *it;
|
||||
}
|
||||
if (sum != 6)
|
||||
return 10;
|
||||
|
||||
sum = 0;
|
||||
for (auto it = arr.rbegin(); it != arr.rend(); ++it) {
|
||||
sum += *it;
|
||||
}
|
||||
if (sum != 6)
|
||||
return 11;
|
||||
}
|
||||
|
||||
// Test const iterators
|
||||
{
|
||||
const ctl::array<int, 3> arr = { 1, 2, 3 };
|
||||
int sum = 0;
|
||||
for (auto it = arr.cbegin(); it != arr.cend(); ++it) {
|
||||
sum += *it;
|
||||
}
|
||||
if (sum != 6)
|
||||
return 12;
|
||||
|
||||
sum = 0;
|
||||
for (auto it = arr.crbegin(); it != arr.crend(); ++it) {
|
||||
sum += *it;
|
||||
}
|
||||
if (sum != 6)
|
||||
return 13;
|
||||
}
|
||||
|
||||
// Test fill method
|
||||
{
|
||||
ctl::array<int, 5> arr;
|
||||
arr.fill(42);
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
if (arr[i] != 42)
|
||||
return 14;
|
||||
}
|
||||
}
|
||||
|
||||
// Test swap method
|
||||
{
|
||||
ctl::array<int, 3> arr1 = { 1, 2, 3 };
|
||||
ctl::array<int, 3> arr2 = { 4, 5, 6 };
|
||||
arr1.swap(arr2);
|
||||
if (arr1[0] != 4 || arr1[1] != 5 || arr1[2] != 6)
|
||||
return 15;
|
||||
if (arr2[0] != 1 || arr2[1] != 2 || arr2[2] != 3)
|
||||
return 16;
|
||||
}
|
||||
|
||||
// Test comparison operators
|
||||
{
|
||||
ctl::array<int, 3> arr1 = { 1, 2, 3 };
|
||||
ctl::array<int, 3> arr2 = { 1, 2, 3 };
|
||||
ctl::array<int, 3> arr3 = { 1, 2, 4 };
|
||||
|
||||
if (!(arr1 == arr2))
|
||||
return 17;
|
||||
if (arr1 != arr2)
|
||||
return 18;
|
||||
if (!(arr1 < arr3))
|
||||
return 19;
|
||||
if (arr3 <= arr1)
|
||||
return 20;
|
||||
if (!(arr3 > arr1))
|
||||
return 21;
|
||||
if (arr1 >= arr3)
|
||||
return 22;
|
||||
}
|
||||
|
||||
// Test non-member swap function
|
||||
{
|
||||
ctl::array<int, 3> arr1 = { 1, 2, 3 };
|
||||
ctl::array<int, 3> arr2 = { 4, 5, 6 };
|
||||
swap(arr1, arr2);
|
||||
if (arr1[0] != 4 || arr1[1] != 5 || arr1[2] != 6)
|
||||
return 23;
|
||||
if (arr2[0] != 1 || arr2[1] != 2 || arr2[2] != 3)
|
||||
return 24;
|
||||
}
|
||||
|
||||
// Test with non-trivial type
|
||||
{
|
||||
struct NonTrivial
|
||||
{
|
||||
int value;
|
||||
|
||||
NonTrivial(int v = 0) : value(v)
|
||||
{
|
||||
}
|
||||
|
||||
bool operator==(const NonTrivial& other) const
|
||||
{
|
||||
return value == other.value;
|
||||
}
|
||||
};
|
||||
|
||||
ctl::array<NonTrivial, 3> arr = { 1, 2, 3 };
|
||||
if (arr[0].value != 1 || arr[1].value != 2 || arr[2].value != 3)
|
||||
return 25;
|
||||
}
|
||||
|
||||
// Test empty array
|
||||
{
|
||||
ctl::array<int, 0> arr;
|
||||
if (!arr.empty())
|
||||
return 26;
|
||||
if (arr.size() != 0)
|
||||
return 27;
|
||||
if (arr.begin() != arr.end())
|
||||
return 28;
|
||||
}
|
||||
|
||||
// Test basic array functionality
|
||||
{
|
||||
ctl::array<int, 5> arr = { 1, 2, 3, 4, 5 };
|
||||
if (arr.size() != 5)
|
||||
return 2;
|
||||
if (arr[0] != 1 || arr[4] != 5)
|
||||
return 3;
|
||||
}
|
||||
|
||||
// Test reverse iterator basics
|
||||
{
|
||||
ctl::array<int, 5> arr = { 1, 2, 3, 4, 5 };
|
||||
auto rit = arr.rbegin();
|
||||
if (*rit != 5)
|
||||
return 4;
|
||||
++rit;
|
||||
if (*rit != 4)
|
||||
return 5;
|
||||
if (*(arr.rbegin() + 2) != 3)
|
||||
return 6;
|
||||
}
|
||||
|
||||
// Test reverse iterator traversal
|
||||
{
|
||||
ctl::array<int, 5> arr = { 1, 2, 3, 4, 5 };
|
||||
int expected = 5;
|
||||
for (auto rit = arr.rbegin(); rit != arr.rend(); ++rit) {
|
||||
if (*rit != expected)
|
||||
return 7;
|
||||
--expected;
|
||||
}
|
||||
}
|
||||
|
||||
// Test const reverse iterator
|
||||
{
|
||||
const ctl::array<int, 5> arr = { 1, 2, 3, 4, 5 };
|
||||
auto crit = arr.crbegin();
|
||||
if (*crit != 5)
|
||||
return 8;
|
||||
++crit;
|
||||
if (*crit != 4)
|
||||
return 9;
|
||||
}
|
||||
|
||||
// Test reverse iterator arithmetic
|
||||
{
|
||||
ctl::array<int, 5> arr = { 1, 2, 3, 4, 5 };
|
||||
auto rit = arr.rbegin();
|
||||
rit += 2;
|
||||
if (*rit != 3)
|
||||
return 10;
|
||||
rit -= 1;
|
||||
if (*rit != 4)
|
||||
return 11;
|
||||
if (*(rit + 2) != 2)
|
||||
return 12;
|
||||
if (*(rit - 1) != 5)
|
||||
return 13;
|
||||
}
|
||||
|
||||
// Test reverse iterator comparison
|
||||
{
|
||||
ctl::array<int, 5> arr = { 1, 2, 3, 4, 5 };
|
||||
auto rit1 = arr.rbegin();
|
||||
auto rit2 = arr.rbegin() + 2;
|
||||
if (rit1 >= rit2)
|
||||
return 14;
|
||||
if (!(rit1 < rit2))
|
||||
return 15;
|
||||
if (rit1 == rit2)
|
||||
return 16;
|
||||
}
|
||||
|
||||
// Test it seems legit
|
||||
{
|
||||
ctl::array<int, 3> arr = { 1, 2, 3 };
|
||||
auto rit = arr.rbegin();
|
||||
if (*rit != 3)
|
||||
return 1;
|
||||
++rit;
|
||||
if (*rit != 2)
|
||||
return 2;
|
||||
++rit;
|
||||
if (*rit != 1)
|
||||
return 3;
|
||||
++rit;
|
||||
if (rit != arr.rend())
|
||||
return 4;
|
||||
}
|
||||
}
|
61
test/ctl/back_inserter_test.cc
Normal file
61
test/ctl/back_inserter_test.cc
Normal file
|
@ -0,0 +1,61 @@
|
|||
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
|
||||
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
||||
//
|
||||
// Copyright 2024 Justine Alexandra Roberts Tunney
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for
|
||||
// any purpose with or without fee is hereby granted, provided that the
|
||||
// above copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include "ctl/array.h"
|
||||
#include "ctl/back_inserter.h"
|
||||
#include "ctl/copy.h"
|
||||
#include "ctl/vector.h"
|
||||
|
||||
// #include <array>
|
||||
// #include <iterator>
|
||||
// #include <vector>
|
||||
// #define ctl std
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
ctl::vector<int> vec = { 1, 2, 3 };
|
||||
ctl::array<int, 3> arr = { 4, 5, 6 };
|
||||
|
||||
// Use back_inserter to append elements from arr to vec
|
||||
ctl::copy(arr.begin(), arr.end(), ctl::back_inserter(vec));
|
||||
|
||||
// Check if vec now contains all elements
|
||||
if (vec.size() != 6)
|
||||
return 1;
|
||||
if (vec[0] != 1 || vec[1] != 2 || vec[2] != 3 || vec[3] != 4 ||
|
||||
vec[4] != 5 || vec[5] != 6)
|
||||
return 2;
|
||||
|
||||
// Use back_inserter with a single element
|
||||
ctl::back_inserter(vec) = 7;
|
||||
|
||||
// Check if the new element was added
|
||||
if (vec.size() != 7)
|
||||
return 3;
|
||||
if (vec[6] != 7)
|
||||
return 4;
|
||||
|
||||
// Test with an empty source range
|
||||
ctl::array<int, 0> empty_arr;
|
||||
ctl::copy(empty_arr.begin(), empty_arr.end(), ctl::back_inserter(vec));
|
||||
|
||||
// Check that no elements were added
|
||||
if (vec.size() != 7)
|
||||
return 5;
|
||||
}
|
66
test/ctl/copy_test.cc
Normal file
66
test/ctl/copy_test.cc
Normal file
|
@ -0,0 +1,66 @@
|
|||
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
|
||||
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
||||
//
|
||||
// Copyright 2024 Justine Alexandra Roberts Tunney
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for
|
||||
// any purpose with or without fee is hereby granted, provided that the
|
||||
// above copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include "ctl/array.h"
|
||||
#include "ctl/copy.h"
|
||||
|
||||
// #include <iterator>
|
||||
// #include <array>
|
||||
// #define ctl std
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
ctl::array<int, 5> src = { 1, 2, 3, 4, 5 };
|
||||
ctl::array<int, 5> dest = { 0, 0, 0, 0, 0 };
|
||||
|
||||
// Test basic copy
|
||||
ctl::copy(src.begin(), src.end(), dest.begin());
|
||||
for (size_t i = 0; i < 5; ++i) {
|
||||
if (dest[i] != src[i])
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Test partial copy
|
||||
ctl::array<int, 5> dest2 = { 0, 0, 0, 0, 0 };
|
||||
ctl::copy(src.begin(), src.begin() + 3, dest2.begin());
|
||||
if (dest2[0] != 1 || dest2[1] != 2 || dest2[2] != 3 || dest2[3] != 0 ||
|
||||
dest2[4] != 0)
|
||||
return 2;
|
||||
|
||||
// Test copy to middle of destination
|
||||
ctl::array<int, 7> dest3 = { 0, 0, 0, 0, 0, 0, 0 };
|
||||
ctl::copy(src.begin(), src.end(), dest3.begin() + 1);
|
||||
if (dest3[0] != 0 || dest3[1] != 1 || dest3[2] != 2 || dest3[3] != 3 ||
|
||||
dest3[4] != 4 || dest3[5] != 5 || dest3[6] != 0)
|
||||
return 3;
|
||||
|
||||
// Test copy with empty range
|
||||
ctl::array<int, 5> dest4 = { 0, 0, 0, 0, 0 };
|
||||
ctl::copy(src.begin(), src.begin(), dest4.begin());
|
||||
for (size_t i = 0; i < 5; ++i) {
|
||||
if (dest4[i] != 0)
|
||||
return 4;
|
||||
}
|
||||
|
||||
// Test copy return value
|
||||
ctl::array<int, 5> dest5 = { 0, 0, 0, 0, 0 };
|
||||
auto result = ctl::copy(src.begin(), src.end(), dest5.begin());
|
||||
if (result != dest5.end())
|
||||
return 5;
|
||||
}
|
|
@ -17,10 +17,9 @@
|
|||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include "ctl/string.h"
|
||||
#include "ctl/type_traits.h"
|
||||
#include "libc/mem/leaks.h"
|
||||
|
||||
#include <__type_traits/is_same.h>
|
||||
|
||||
#include "libc/str/str.h"
|
||||
|
||||
// #include <string>
|
||||
|
@ -366,7 +365,7 @@ main()
|
|||
|
||||
{
|
||||
String s;
|
||||
if constexpr (std::is_same_v<ctl::string, decltype(s)>) {
|
||||
if constexpr (ctl::is_same_v<ctl::string, decltype(s)>) {
|
||||
// tests the small-string optimization on ctl::string
|
||||
for (int i = 0; i < 23; ++i) {
|
||||
s.append("a");
|
||||
|
@ -397,7 +396,7 @@ main()
|
|||
s.resize(4);
|
||||
if (s != "arst")
|
||||
return 105;
|
||||
if constexpr (std::is_same_v<ctl::string, decltype(s)>) {
|
||||
if constexpr (ctl::is_same_v<ctl::string, decltype(s)>) {
|
||||
String r(s);
|
||||
if (issmall(s) || !issmall(r))
|
||||
return 106;
|
||||
|
@ -405,5 +404,4 @@ main()
|
|||
}
|
||||
|
||||
CheckForMemoryLeaks();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -16,11 +16,12 @@
|
|||
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include "ctl/type_traits.h"
|
||||
#include "ctl/unique_ptr.h"
|
||||
|
||||
#include <__type_traits/is_same.h>
|
||||
#include "libc/mem/leaks.h"
|
||||
|
||||
// #include <memory>
|
||||
// #include <type_traits>
|
||||
// #define ctl std
|
||||
|
||||
template<typename T, typename D = ctl::default_delete<T>>
|
||||
|
@ -70,7 +71,7 @@ struct FinalDeleter final
|
|||
static_assert(sizeof(Ptr<int, SetsGDeleter>) == sizeof(int*));
|
||||
|
||||
// not everyone uses [[no_unique_address]]...
|
||||
static_assert(!std::is_same_v<Ptr<int>, ctl::unique_ptr<int>> ||
|
||||
static_assert(!ctl::is_same_v<Ptr<int>, ctl::unique_ptr<int>> ||
|
||||
sizeof(Ptr<int, FinalDeleter>) == sizeof(int*));
|
||||
|
||||
struct SetsGCtor
|
||||
|
@ -227,6 +228,4 @@ main()
|
|||
|
||||
// TODO(mrdomino): Fix memory leaks reported by MODE=dbg
|
||||
// CheckForMemoryLeaks();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -16,13 +16,10 @@
|
|||
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include "ctl/string.h"
|
||||
#include "ctl/vector.h"
|
||||
#include "libc/mem/leaks.h"
|
||||
|
||||
#include <cosmo.h>
|
||||
|
||||
#include "ctl/string.h"
|
||||
|
||||
// #include <string>
|
||||
// #include <vector>
|
||||
// #define ctl std
|
||||
|
@ -315,6 +312,45 @@ main()
|
|||
return 69;
|
||||
}
|
||||
|
||||
{
|
||||
ctl::vector<int> A = { 1, 2, 3 };
|
||||
if (A[1] != 2)
|
||||
return 70;
|
||||
A = { 4, 5, 6 };
|
||||
if (A[1] != 5)
|
||||
return 71;
|
||||
}
|
||||
|
||||
{
|
||||
ctl::vector<int> arr = { 1, 2, 3 };
|
||||
auto rit = arr.rbegin();
|
||||
if (*rit != 3)
|
||||
return 72;
|
||||
++rit;
|
||||
if (*rit != 2)
|
||||
return 73;
|
||||
++rit;
|
||||
if (*rit != 1)
|
||||
return 74;
|
||||
++rit;
|
||||
if (rit != arr.rend())
|
||||
return 75;
|
||||
}
|
||||
|
||||
{
|
||||
ctl::vector<ctl::string> A = { "hi", "theretheretheretherethere" };
|
||||
if (A.size() != 2)
|
||||
return 76;
|
||||
if (A[0] != "hi")
|
||||
return 77;
|
||||
if (A[1] != "theretheretheretherethere")
|
||||
return 78;
|
||||
A = { "theretheretheretherethere", "hi" };
|
||||
if (A[0] != "theretheretheretherethere")
|
||||
return 79;
|
||||
if (A[1] != "hi")
|
||||
return 80;
|
||||
}
|
||||
|
||||
CheckForMemoryLeaks();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -144,7 +144,6 @@ static void *pthread_main(void *ptr) {
|
|||
struct State *s = ptr;
|
||||
struct State children[2];
|
||||
int fd, rc;
|
||||
|
||||
fd = s->fd;
|
||||
if (s->id < 8) {
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
|
|
25
test/libc/stdio/lemur64_test.c
Normal file
25
test/libc/stdio/lemur64_test.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2024 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(lemur64, test) {
|
||||
EXPECT_EQ(1819718037028923529, lemur64());
|
||||
EXPECT_EQ(-3120132252617434764, lemur64());
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue