Get rid of preprocessor stuff in test (#1202)

Also it's a bit more idiomatic to say s.npos rather than string::npos.
This commit is contained in:
Jōshin 2024-06-10 07:00:37 -07:00 committed by GitHub
parent 32643e9fa7
commit 0dde3a0e70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -24,15 +24,16 @@
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
#include "libc/str/str.h" #include "libc/str/str.h"
using String = ctl::string;
// #include <string> // #include <string>
// #define ctl std // using String = std::string;
int int
main() main()
{ {
{ {
ctl::string s; String s;
s += 'h'; s += 'h';
s += 'i'; s += 'i';
if (s != "hi") if (s != "hi")
@ -40,7 +41,7 @@ main()
} }
{ {
ctl::string s; String s;
if (!s.empty()) if (!s.empty())
return 6; return 6;
s.reserve(32); s.reserve(32);
@ -56,7 +57,7 @@ main()
} }
{ {
ctl::string s; String s;
s += "hello world how are you"; s += "hello world how are you";
s.reserve(3); s.reserve(3);
if (s != "hello world how are you") if (s != "hello world how are you")
@ -64,7 +65,7 @@ main()
} }
{ {
ctl::string s(4, 'x'); String s(4, 'x');
if (s != "xxxx") if (s != "xxxx")
return 12; return 12;
s.resize(3); s.resize(3);
@ -76,42 +77,42 @@ main()
} }
{ {
ctl::string a = "a"; String a = "a";
ctl::string b = "a"; String b = "a";
if (a.compare(b) != 0) if (a.compare(b) != 0)
return 17; return 17;
} }
{ {
ctl::string a = "a"; String a = "a";
ctl::string b = "b"; String b = "b";
if (a.compare(b) >= 0) if (a.compare(b) >= 0)
return 18; return 18;
} }
{ {
ctl::string a = "a"; String a = "a";
ctl::string b = "ab"; String b = "ab";
if (a.compare(b) >= 0) if (a.compare(b) >= 0)
return 19; return 19;
} }
{ {
ctl::string a = "ab"; String a = "ab";
ctl::string b = "a"; String b = "a";
if (a.compare(b) <= 0) if (a.compare(b) <= 0)
return 20; return 20;
} }
{ {
ctl::string a = ""; String a = "";
ctl::string b = ""; String b = "";
if (a.compare(b) != 0) if (a.compare(b) != 0)
return 21; return 21;
} }
{ {
ctl::string a = "fooBARbaz"; String a = "fooBARbaz";
if (a.substr(3, 3) != "BAR") if (a.substr(3, 3) != "BAR")
return 22; return 22;
if (a.replace(3, 3, "MOO") != "fooMOObaz") if (a.replace(3, 3, "MOO") != "fooMOObaz")
@ -119,7 +120,7 @@ main()
} }
{ {
ctl::string a = "fooBAR"; String a = "fooBAR";
if (a.substr(3, 3) != "BAR") if (a.substr(3, 3) != "BAR")
return 24; return 24;
if (a.replace(3, 3, "MOO") != "fooMOO") if (a.replace(3, 3, "MOO") != "fooMOO")
@ -127,7 +128,7 @@ main()
} }
{ {
ctl::string a = "fooBAR"; String a = "fooBAR";
if (a.substr(1, 0) != "") if (a.substr(1, 0) != "")
return 26; return 26;
if (a.replace(1, 0, "MOO") != "fMOOooBAR") if (a.replace(1, 0, "MOO") != "fMOOooBAR")
@ -143,15 +144,15 @@ main()
} }
{ {
ctl::string s1 = "hello"; String s1 = "hello";
ctl::string s2 = "world"; String s2 = "world";
ctl::string s3 = s1 + " " + s2; String s3 = s1 + " " + s2;
if (s3 != "hello world") if (s3 != "hello world")
return 32; return 32;
} }
{ {
ctl::string s = "hello"; String s = "hello";
if (s.size() != 5) if (s.size() != 5)
return 33; return 33;
if (s.length() != 5) if (s.length() != 5)
@ -161,7 +162,7 @@ main()
} }
{ {
ctl::string s = "hello"; String s = "hello";
if (s[0] != 'h' || s[1] != 'e' || s[2] != 'l' || s[3] != 'l' || if (s[0] != 'h' || s[1] != 'e' || s[2] != 'l' || s[3] != 'l' ||
s[4] != 'o') s[4] != 'o')
return 36; return 36;
@ -171,17 +172,17 @@ main()
} }
{ {
ctl::string s = "hello"; String s = "hello";
if (s.find('e') != 1) if (s.find('e') != 1)
return 38; return 38;
if (s.find('l') != 2) if (s.find('l') != 2)
return 39; return 39;
if (s.find('x') != ctl::string::npos) if (s.find('x') != s.npos)
return 40; return 40;
} }
{ {
ctl::string s = "hello"; String s = "hello";
if (!s.ends_with("lo")) if (!s.ends_with("lo"))
return 41; return 41;
if (s.ends_with("el")) if (s.ends_with("el"))
@ -189,8 +190,8 @@ main()
} }
{ {
ctl::string s = "hello"; String s = "hello";
ctl::string sub = s.substr(1, 3); String sub = s.substr(1, 3);
if (sub != "ell") if (sub != "ell")
return 43; return 43;
sub = s.substr(2); sub = s.substr(2);
@ -199,8 +200,8 @@ main()
} }
{ {
ctl::string s = "hello"; String s = "hello";
ctl::string s2 = s; String s2 = s;
if (s != s2) if (s != s2)
return 45; return 45;
s2[0] = 'H'; s2[0] = 'H';
@ -209,8 +210,8 @@ main()
} }
{ {
ctl::string s = "hello"; String s = "hello";
ctl::string s2 = std::move(s); String s2 = std::move(s);
if (s2 != "hello") if (s2 != "hello")
return 47; return 47;
if (!s.empty()) if (!s.empty())
@ -218,14 +219,14 @@ main()
} }
{ {
ctl::string s = "hello"; String s = "hello";
const char* cstr = s.c_str(); const char* cstr = s.c_str();
if (strcmp(cstr, "hello") != 0) if (strcmp(cstr, "hello") != 0)
return 49; return 49;
} }
// { // {
// ctl::string s = "hello"; // String s = "hello";
// char buffer[10]; // char buffer[10];
// s.copy(buffer, sizeof(buffer)); // s.copy(buffer, sizeof(buffer));
// if (strcmp(buffer, "hello") != 0) // if (strcmp(buffer, "hello") != 0)
@ -233,7 +234,7 @@ main()
// } // }
{ {
ctl::string s = "hello"; String s = "hello";
s.resize(3); s.resize(3);
if (s != "hel") if (s != "hel")
return 51; return 51;
@ -243,14 +244,14 @@ main()
} }
{ {
ctl::string s = "hello"; String s = "hello";
s.clear(); s.clear();
if (!s.empty()) if (!s.empty())
return 53; return 53;
} }
{ {
ctl::string s = "hello"; String s = "hello";
auto it = s.begin(); auto it = s.begin();
if (*it != 'h') if (*it != 'h')
return 54; return 54;
@ -260,21 +261,21 @@ main()
} }
// { // {
// ctl::string s = "hello"; // String s = "hello";
// ctl::string s2 = "world"; // String s2 = "world";
// s.swap(s2); // s.swap(s2);
// if (s != "world" || s2 != "hello") // if (s != "world" || s2 != "hello")
// return 56; // return 56;
// } // }
{ {
ctl::string s = "hello"; String s = "hello";
if (s.front() != 'h' || s.back() != 'o') if (s.front() != 'h' || s.back() != 'o')
return 57; return 57;
} }
{ {
ctl::string s = "hello"; String s = "hello";
s.push_back('!'); s.push_back('!');
if (s != "hello!") if (s != "hello!")
return 58; return 58;
@ -284,28 +285,28 @@ main()
} }
{ {
ctl::string s = "hello"; String s = "hello";
s.insert(2, "XYZ"); s.insert(2, "XYZ");
if (s != "heXYZllo") if (s != "heXYZllo")
return 60; return 60;
} }
{ {
ctl::string s = "hello"; String s = "hello";
s.erase(1, 2); s.erase(1, 2);
if (s != "hlo") if (s != "hlo")
return 61; return 61;
} }
{ {
ctl::string s = "hello"; String s = "hello";
s.replace(1, 2, "XYZ"); s.replace(1, 2, "XYZ");
if (s != "hXYZlo") if (s != "hXYZlo")
return 62; return 62;
} }
{ {
ctl::string s = "hello"; String s = "hello";
s.append(" world"); s.append(" world");
if (s != "hello world") if (s != "hello world")
return 63; return 63;
@ -319,14 +320,14 @@ main()
} }
// { // {
// ctl::string s = "hello"; // String s = "hello";
// s.assign("world"); // s.assign("world");
// if (s != "world") // if (s != "world")
// return 64; // return 64;
// } // }
{ {
ctl::string s = "hello"; String s = "hello";
if (s.compare("world") >= 0) if (s.compare("world") >= 0)
return 65; return 65;
if (s.compare("hello") != 0) if (s.compare("hello") != 0)
@ -336,7 +337,7 @@ main()
} }
{ {
ctl::string s = "hello"; String s = "hello";
if (s == "world") if (s == "world")
return 68; return 68;
if (s != "hello") if (s != "hello")
@ -355,8 +356,7 @@ main()
} }
{ {
ctl::string s; String s;
#undef ctl
if constexpr (std::is_same_v<ctl::string, decltype(s)>) { if constexpr (std::is_same_v<ctl::string, decltype(s)>) {
// tests the small-string optimization on ctl::string // tests the small-string optimization on ctl::string
char* d = s.data(); char* d = s.data();