Introduce Cosmopolitan Templates Library (CTL)

This commit is contained in:
Justine Tunney 2024-06-03 09:09:33 -07:00
parent b003888696
commit 4937843f70
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
16 changed files with 7054 additions and 12 deletions

12
test/ctl/.clang-format Normal file
View file

@ -0,0 +1,12 @@
---
BasedOnStyle: Mozilla
IndentWidth: 4
ColumnLimit: 80
---
Language: Cpp
AllowShortFunctionsOnASingleLine: false
AlignTrailingComments: false
AlignEscapedNewlines: DontAlign
AlwaysBreakTemplateDeclarations: true
ConstructorInitializerAllOnOneLineOrOnePerLine: true
---

38
test/ctl/BUILD.mk Normal file
View file

@ -0,0 +1,38 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘
PKGS += TEST_CTL
TEST_CTL_FILES := $(wildcard test/ctl/*)
TEST_CTL_SRCS = $(filter %.cc,$(TEST_CTL_FILES))
TEST_CTL_OBJS = $(TEST_CTL_SRCS:%.cc=o/$(MODE)/%.o)
TEST_CTL_COMS = $(TEST_CTL_OBJS:%.o=%)
TEST_CTL_BINS = $(TEST_CTL_COMS) $(TEST_CTL_COMS:%=%.dbg)
TEST_CTL_CHECKS = $(TEST_CTL_COMS:%=%.runs)
TEST_CTL_TESTS = $(TEST_CTL_COMS:%=%.ok)
TEST_CTL_DIRECTDEPS = \
CTL \
LIBC_INTRIN \
LIBC_LOG \
THIRD_PARTY_LIBCXX \
TEST_CTL_DEPS := \
$(call uniq,$(foreach x,$(TEST_CTL_DIRECTDEPS),$($(x))))
o/$(MODE)/test/ctl/ctl.pkg: \
$(TEST_CTL_OBJS) \
$(foreach x,$(TEST_CTL_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/test/ctl/%.dbg: \
$(TEST_CTL_DEPS) \
o/$(MODE)/test/ctl/%.o \
o/$(MODE)/test/ctl/ctl.pkg \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
.PHONY: o/$(MODE)/test/ctl
o/$(MODE)/test/ctl: \
$(TEST_CTL_BINS) \
$(TEST_CTL_CHECKS)

114
test/ctl/optional_test.cc Normal file
View file

@ -0,0 +1,114 @@
// -*- 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 Mozilla Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ctl/optional.h"
#include <new>
#include "ctl/string.h"
int
main()
{
{
Optional<int> x;
if (x)
return 1;
if (x.has_value())
return 2;
}
{
Optional<int> x(42);
if (!x)
return 3;
if (!x.has_value())
return 4;
if (x.value() != 42)
return 5;
}
{
Optional<String> x("hello");
Optional<String> y(x);
if (!y)
return 6;
if (!y.has_value())
return 7;
if (y.value() != "hello")
return 8;
}
{
Optional<String> x("world");
Optional<String> y(std::move(x));
if (!y)
return 9;
if (!y.has_value())
return 10;
if (y.value() != "world")
return 11;
}
{
Optional<int> x(42);
Optional<int> y;
y = x;
if (!y)
return 13;
if (!y.has_value())
return 14;
if (y.value() != 42)
return 15;
}
{
Optional<String> x("hello");
Optional<String> y;
y = std::move(x);
if (!y)
return 16;
if (!y.has_value())
return 17;
if (y.value() != "hello")
return 18;
}
{
Optional<int> x(42);
x.reset();
if (x)
return 20;
if (x.has_value())
return 21;
}
{
Optional<String> x;
x.emplace("hello");
if (!x)
return 22;
if (!x.has_value())
return 23;
if (x.value() != "hello")
return 24;
}
CheckForMemoryLeaks();
return 0;
}

371
test/ctl/string_test.cc Normal file
View file

@ -0,0 +1,371 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8
//
// 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 <__utility/move.h>
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
// #include <string>
// #define String std::string
int
main(int argc, char* argv[])
{
{
String s;
s += 'h';
s += 'i';
if (s != "hi")
return 1;
}
{
String s;
if (!s.empty())
return 6;
s.reserve(32);
if (!s.empty())
return 7;
if (!s.starts_with(""))
return 8;
if (s.starts_with("a"))
return 9;
s += "abc";
if (!s.starts_with("a"))
return 10;
}
{
String s;
s += "hello world how are you";
s.reserve(3);
if (s != "hello world how are you")
return 11;
}
{
String s(4, 'x');
if (s != "xxxx")
return 12;
s.resize(3);
if (s != "xxx")
return 13;
s.resize(4, 'y');
if (s != "xxxy")
return 14;
}
{
String a = "a";
String b = "a";
if (a.compare(b) != 0)
return 17;
}
{
String a = "a";
String b = "b";
if (a.compare(b) >= 0)
return 18;
}
{
String a = "a";
String b = "ab";
if (a.compare(b) >= 0)
return 19;
}
{
String a = "ab";
String b = "a";
if (a.compare(b) <= 0)
return 20;
}
{
String a = "";
String b = "";
if (a.compare(b) != 0)
return 21;
}
{
String a = "fooBARbaz";
if (a.substr(3, 3) != "BAR")
return 22;
if (a.replace(3, 3, "MOO") != "fooMOObaz")
return 23;
}
{
String a = "fooBAR";
if (a.substr(3, 3) != "BAR")
return 24;
if (a.replace(3, 3, "MOO") != "fooMOO")
return 25;
}
{
String a = "fooBAR";
if (a.substr(1, 0) != "")
return 26;
if (a.replace(1, 0, "MOO") != "fMOOooBAR")
return 27;
if (!a.starts_with("fMOOooBAR"))
return 28;
if (!a.ends_with(""))
return 29;
if (!a.ends_with("BAR"))
return 30;
if (a.ends_with("bar"))
return 31;
}
{
String s1 = "hello";
String s2 = "world";
String s3 = s1 + " " + s2;
if (s3 != "hello world")
return 32;
}
{
String s = "hello";
if (s.size() != 5)
return 33;
if (s.length() != 5)
return 34;
if (s.capacity() < 5)
return 35;
}
{
String s = "hello";
if (s[0] != 'h' || s[1] != 'e' || s[2] != 'l' || s[3] != 'l' ||
s[4] != 'o')
return 36;
s[0] = 'H';
if (s != "Hello")
return 37;
}
{
String s = "hello";
if (s.find('e') != 1)
return 38;
if (s.find('l') != 2)
return 39;
if (s.find('x') != String::npos)
return 40;
}
{
String s = "hello";
if (!s.ends_with("lo"))
return 41;
if (s.ends_with("el"))
return 42;
}
{
String s = "hello";
String sub = s.substr(1, 3);
if (sub != "ell")
return 43;
sub = s.substr(2);
if (sub != "llo")
return 44;
}
{
String s = "hello";
String s2 = s;
if (s != s2)
return 45;
s2[0] = 'H';
if (s == s2)
return 46;
}
{
String s = "hello";
String s2 = std::move(s);
if (s2 != "hello")
return 47;
if (!s.empty())
return 48;
}
{
String s = "hello";
const char* cstr = s.c_str();
if (strcmp(cstr, "hello") != 0)
return 49;
}
// {
// String s = "hello";
// char buffer[10];
// s.copy(buffer, sizeof(buffer));
// if (strcmp(buffer, "hello") != 0)
// return 50;
// }
{
String s = "hello";
s.resize(3);
if (s != "hel")
return 51;
s.resize(10, 'x');
if (s != "helxxxxxxx")
return 52;
}
{
String s = "hello";
s.clear();
if (!s.empty())
return 53;
}
{
String s = "hello";
auto it = s.begin();
if (*it != 'h')
return 54;
++it;
if (*it != 'e')
return 55;
}
// {
// String s = "hello";
// String s2 = "world";
// s.swap(s2);
// if (s != "world" || s2 != "hello")
// return 56;
// }
{
String s = "hello";
if (s.front() != 'h' || s.back() != 'o')
return 57;
}
{
String s = "hello";
s.push_back('!');
if (s != "hello!")
return 58;
s.pop_back();
if (s != "hello")
return 59;
}
{
String s = "hello";
s.insert(2, "XYZ");
if (s != "heXYZllo")
return 60;
}
{
String s = "hello";
s.erase(1, 2);
if (s != "hlo")
return 61;
}
{
String s = "hello";
s.replace(1, 2, "XYZ");
if (s != "hXYZlo")
return 62;
}
{
String s = "hello";
s.append(" world");
if (s != "hello world")
return 63;
}
// {
// String s = "hello";
// s.assign("world");
// if (s != "world")
// return 64;
// }
{
String s = "hello";
if (s.compare("world") >= 0)
return 65;
if (s.compare("hello") != 0)
return 66;
if (s.compare("hallo") <= 0)
return 67;
}
{
String s = "hello";
if (s == "world")
return 68;
if (s != "hello")
return 69;
if (s < "hallo")
return 70;
if (s > "world")
return 71;
}
{
StringView s = "hello";
if (s.find('e') != 1)
return 72;
if (s.find('l') != 2)
return 73;
if (s.find('x') != String::npos)
return 74;
}
{
StringView s = "hello there";
s.remove_prefix(6);
if (s != "there")
return 75;
s.remove_suffix(1);
if (s != "ther")
return 76;
}
{
if ("hello"s != "hello")
return 77;
if ("hell"s + "o" != "hello")
return 78;
}
CheckForMemoryLeaks();
return 0;
}

320
test/ctl/vector_test.cc Normal file
View file

@ -0,0 +1,320 @@
// -*- 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 Mozilla Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ctl/vector.h"
#include <cosmo.h>
#include <new>
#include "ctl/string.h"
// #include <string>
// #include <vector>
// #define String std::string
// #define Vector std::vector
int
main(int argc, char* argv[])
{
{
int x = 3;
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;
}
{
String yo = "foo";
Vector<String> A;
A.push_back("fun");
A.push_back(std::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;
}
{
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;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B(A);
if (B.size() != 3)
return 15;
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
return 16;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B(std::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;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
B = A;
if (B.size() != 3)
return 20;
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
return 21;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
B = std::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;
}
{
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;
}
{
Vector<int> A;
A.resize(5);
if (A.size() != 5)
return 27;
A.resize(3);
if (A.size() != 3)
return 28;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
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;
}
{
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;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
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;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
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;
}
{
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;
}
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
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;
}
{
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;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
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;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
B = std::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;
}
{
Vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
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;
}
CheckForMemoryLeaks();
return 0;
}