mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 15:03:34 +00:00
38921dc46b
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.
356 lines
7.9 KiB
C++
356 lines
7.9 KiB
C++
// -*- 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/string.h"
|
|
#include "ctl/vector.h"
|
|
#include "libc/mem/leaks.h"
|
|
|
|
// #include <string>
|
|
// #include <vector>
|
|
// #define ctl std
|
|
|
|
int
|
|
main()
|
|
{
|
|
|
|
{
|
|
int x = 3;
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(x);
|
|
if (A[0] != 1)
|
|
return 1;
|
|
if (A[1] != 2)
|
|
return 2;
|
|
if (A[2] != 3)
|
|
return 3;
|
|
if (A.size() != 3)
|
|
return 4;
|
|
}
|
|
|
|
{
|
|
ctl::string yo = "foo";
|
|
ctl::vector<ctl::string> A;
|
|
A.push_back("fun");
|
|
A.push_back(ctl::move(yo));
|
|
if (yo != "")
|
|
return 5;
|
|
A.emplace_back("bar");
|
|
if (A[0] != "fun")
|
|
return 7;
|
|
if (A[1] != "foo")
|
|
return 8;
|
|
if (A[2] != "bar")
|
|
return 9;
|
|
if (A.size() != 3)
|
|
return 10;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
if (!A.empty())
|
|
return 11;
|
|
A.push_back(5);
|
|
if (A.empty())
|
|
return 12;
|
|
if (A.front() != 5)
|
|
return 13;
|
|
if (A.back() != 5)
|
|
return 14;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
ctl::vector<int> B(A);
|
|
if (B.size() != 3)
|
|
return 15;
|
|
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
|
|
return 16;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
ctl::vector<int> B(ctl::move(A));
|
|
if (A.size() != 0)
|
|
return 17;
|
|
if (B.size() != 3)
|
|
return 18;
|
|
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
|
|
return 19;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
ctl::vector<int> B;
|
|
B = A;
|
|
if (B.size() != 3)
|
|
return 20;
|
|
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
|
|
return 21;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
ctl::vector<int> B;
|
|
B = ctl::move(A);
|
|
if (A.size() != 0)
|
|
return 22;
|
|
if (B.size() != 3)
|
|
return 23;
|
|
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
|
|
return 24;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
A.pop_back();
|
|
if (A.size() != 2)
|
|
return 25;
|
|
if (A[0] != 1 || A[1] != 2)
|
|
return 26;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.resize(5);
|
|
if (A.size() != 5)
|
|
return 27;
|
|
A.resize(3);
|
|
if (A.size() != 3)
|
|
return 28;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
ctl::vector<int> B;
|
|
B.push_back(4);
|
|
B.push_back(5);
|
|
A.swap(B);
|
|
if (A.size() != 2)
|
|
return 29;
|
|
if (B.size() != 3)
|
|
return 30;
|
|
if (A[0] != 4 || A[1] != 5)
|
|
return 31;
|
|
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
|
|
return 32;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
A.clear();
|
|
if (A.size() != 0)
|
|
return 33;
|
|
if (!A.empty())
|
|
return 34;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
ctl::vector<int>::iterator it = A.begin();
|
|
if (*it != 1)
|
|
return 35;
|
|
++it;
|
|
if (*it != 2)
|
|
return 36;
|
|
++it;
|
|
if (*it != 3)
|
|
return 37;
|
|
++it;
|
|
if (it != A.end())
|
|
return 38;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
ctl::vector<int>::const_iterator cit = A.cbegin();
|
|
if (*cit != 1)
|
|
return 39;
|
|
++cit;
|
|
if (*cit != 2)
|
|
return 40;
|
|
++cit;
|
|
if (*cit != 3)
|
|
return 41;
|
|
++cit;
|
|
if (cit != A.cend())
|
|
return 42;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
for (int i = 0; i < 100; ++i) {
|
|
A.push_back(i);
|
|
}
|
|
if (A.size() != 100)
|
|
return 51;
|
|
for (int i = 0; i < 100; ++i) {
|
|
if (A[i] != i)
|
|
return 52;
|
|
}
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
ctl::vector<int> B(A);
|
|
if (B.size() != 3)
|
|
return 53;
|
|
B.push_back(4);
|
|
if (A.size() != 3)
|
|
return 54;
|
|
if (B.size() != 4)
|
|
return 55;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.reserve(100);
|
|
if (A.size() != 0)
|
|
return 56;
|
|
if (A.capacity() != 100)
|
|
return 57;
|
|
A.push_back(1);
|
|
if (A.size() != 1)
|
|
return 58;
|
|
if (A.capacity() != 100)
|
|
return 59;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
ctl::vector<int> B;
|
|
B = A;
|
|
if (B.size() != 3)
|
|
return 60;
|
|
B.push_back(4);
|
|
if (A.size() != 3)
|
|
return 61;
|
|
if (B.size() != 4)
|
|
return 62;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
ctl::vector<int> B;
|
|
B = ctl::move(A);
|
|
if (A.size() != 0)
|
|
return 63;
|
|
if (B.size() != 3)
|
|
return 64;
|
|
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
|
|
return 65;
|
|
}
|
|
|
|
{
|
|
ctl::vector<int> A;
|
|
A.push_back(1);
|
|
A.push_back(2);
|
|
A.push_back(3);
|
|
ctl::vector<int> B;
|
|
B.push_back(4);
|
|
B.push_back(5);
|
|
A.swap(B);
|
|
if (A.size() != 2)
|
|
return 66;
|
|
if (B.size() != 3)
|
|
return 67;
|
|
if (A[0] != 4 || A[1] != 5)
|
|
return 68;
|
|
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
|
|
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();
|
|
}
|