SHA256
108 lines
2.9 KiB
C++
108 lines
2.9 KiB
C++
#include <bstl/string.hpp>
|
|
|
|
#include <cassert>
|
|
#include <iostream>
|
|
|
|
int main() {
|
|
using bstl::String;
|
|
|
|
// 基本构造和获取
|
|
String s1("Hello, World!");
|
|
assert(s1.length() == 13);
|
|
assert(s1.c_str() == std::string("Hello, World!"));
|
|
|
|
// split 分割
|
|
String s2("apple,banana,cherry");
|
|
auto parts = s2.split(",");
|
|
assert(parts.size() == 3);
|
|
assert(parts[0] == String("apple"));
|
|
assert(parts[1] == String("banana"));
|
|
assert(parts[2] == String("cherry"));
|
|
|
|
// join 连接
|
|
std::vector<String> words = {String("Hello"), String("World")};
|
|
String joined = String::join(words, " ");
|
|
assert(joined == String("Hello World"));
|
|
|
|
// replace 替换
|
|
String s3("hello hello world");
|
|
String replaced = s3.replace("hello", "hi");
|
|
assert(replaced == String("hi hi world"));
|
|
|
|
String replaced1 = s3.replace("hello", "hi", 1);
|
|
assert(replaced1 == String("hi hello world"));
|
|
|
|
// upper/lower
|
|
String s4("HeLLo WoRLd");
|
|
assert(s4.upper() == String("HELLO WORLD"));
|
|
assert(s4.lower() == String("hello world"));
|
|
|
|
// capitalize
|
|
String s5("hello");
|
|
assert(s5.capitalize() == String("Hello"));
|
|
|
|
// title
|
|
String s6("hello world");
|
|
assert(s6.title() == String("Hello World"));
|
|
|
|
// startswith/endswith
|
|
String s7("hello.txt");
|
|
assert(s7.startswith("hello"));
|
|
assert(s7.endswith(".txt"));
|
|
assert(!s7.startswith(".txt"));
|
|
|
|
// contains
|
|
assert(s7.contains("llo"));
|
|
assert(!s7.contains("xyz"));
|
|
|
|
// isdigit/isalpha/isalnum
|
|
assert(String("12345").isdigit());
|
|
assert(!String("123a").isdigit());
|
|
assert(String("abc").isalpha());
|
|
assert(!String("abc1").isalpha());
|
|
assert(String("abc123").isalnum());
|
|
|
|
// isupper/islower
|
|
assert(String("HELLO").isupper());
|
|
assert(String("hello").islower());
|
|
assert(!String("Hello").isupper());
|
|
|
|
// find/rfind/count
|
|
assert(s2.find("banana") == 6);
|
|
assert(s2.find("xyz") == -1);
|
|
assert(s3.count("hello") == 2);
|
|
|
|
// strip
|
|
String s8(" hello world ");
|
|
assert(s8.strip() == String("hello world"));
|
|
assert(s8.lstrip() == String("hello world "));
|
|
assert(s8.rstrip() == String(" hello world"));
|
|
|
|
// ljust/rjust/center
|
|
String s9("hi");
|
|
assert(s9.ljust(5, '.') == String("hi..."));
|
|
assert(s9.rjust(5, '.') == String("...hi"));
|
|
assert(s9.center(5, '.') == String(".hi.."));
|
|
|
|
// 字符串转换
|
|
assert(String("123").toInt() == 123);
|
|
assert(String("456789").toLongLong() == 456789);
|
|
assert(String("3.14").toDouble() == 3.14);
|
|
|
|
// 运算符
|
|
String sa("Hello");
|
|
String sb(" World");
|
|
String sc = sa + sb;
|
|
assert(sc == String("Hello World"));
|
|
|
|
sa += sb;
|
|
assert(sa == String("Hello World"));
|
|
|
|
// 比较
|
|
assert(String("abc") < String("def"));
|
|
assert(String("xyz") > String("abc"));
|
|
|
|
std::cout << "All String tests passed!\n";
|
|
return 0;
|
|
}
|