📄 strtst.cpp
字号:
// test character string manipulation library// Copyright (c) R B Davies 1996#define WANT_STREAM#include "include.h"#include "str.h"#include "str_fns.h"#include <fstream.h>String GlobalString1; // make sure globals are OKString GlobalString2 = "Not null";String GlobalString3 = "";class Counter{ unsigned int i; const char* s;public: Counter(char* S) : i(0), s(S) {} ~Counter(); void operator++() { i++; } unsigned int Value() const { return i; }};Counter::~Counter() { cout << s << " " << i << "\n"; }Counter Totals("Number of tests ..............:");Counter Errors("Number of errors (should be 1):");void prstr(const String& s, const String& t);void compare(long i, long j);void compare_bool(bool i, bool j);void compare(const void* i, const void* j, bool agree=true);String thrice(const String& X){ String A; A += X; A += '/'; A += X; A += '/'; A += X; A += '.'; return A;}String twice(const String& X) { String A = X + "/"; String B = X + "."; return A + B; }String alphabet1(String*& B){ String A(26, (char)0); for (int i = 0; i < 26; i++) A[i] = (char)('A' + i); B = new String(A); return A;}String alphabet2(String*& B){ String A = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; B = new String(A); return A;}void none(const String&) {}// test change of string in function - see if memory is returnedvoid MemRet(String& TheString){ String NewString = "The new string"; TheString = NewString;}int main(){ const char* s1; const char* s2; const char* s3; const char* s4; cout << "\nBegin test\n"; // Forces cout to allocate memory at beginning { String str; str.reserve(30000); s1 = str.data(); } { String str = "a"; s3 = str.data(); } { cout << "testing test program - should report error\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; prstr("just testing - 1", "just testing - 2 [0]"); cout << "preliminary tests\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; { String f = "123456"; prstr(f, "123456 [0]"); f = "abcdefg"; prstr(f, "abcdefg [0]"); String g = "hijklmn"; prstr(f + g, "abcdefghijklmn [0]"); f.reserve(30000); prstr(f, "abcdefg [0]"); const char* s = f.c_str(); prstr(s, "abcdefg [0]"); String h = f; prstr(h, "abcdefg [0]"); String j; j = h; prstr(h, "abcdefg [1]"); prstr(j, "abcdefg [1]"); compare_bool(h==j, true); f = "abcdef"; prstr("12345" + f, "12345abcdef [0]"); prstr(f + "12345", "abcdef12345 [0]"); f = "123"; g = "456"; h = "789"; String fgh = f + g + h; prstr(fgh, "123456789 [0]"); fgh = "abc"; prstr(fgh, "abc [0]"); prstr(f + g + h, "123456789 [0]"); prstr(fgh, "abc [0]"); } cout << "test string creation and copying\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; String f = "0123456789"; // creates string "0123456789" prstr(f, "0123456789 [0]"); String g = f; prstr(g, "0123456789 [1]"); prstr(f, "0123456789 [1]"); g = "abc"; String null1; prstr(g, "abc [0]"); prstr(f, "0123456789 [0]"); g = f; prstr(g, "0123456789 [1]"); prstr(f, "0123456789 [1]"); compare(f.size(), 10); compare(f.length(), 10); compare(f.capacity(), 10); compare(f.max_size(), String::npos - 1); f.reserve(30); prstr(g, "0123456789 [0]"); prstr(f, "0123456789 [0]"); compare(f.size(), 10); compare(f.length(), 10); compare(f.capacity(), 30); f.reserve(20); compare(f.capacity(), 20); f.reserve(5); compare(f.capacity(), 10); f.reserve(20); String h = "pqr"; const void* fd = f.data(); const void* gd = g.data(); f += h; g += h; compare(f.capacity(), 20); compare(fd, f.data(), true); compare(gd, g.data(), false); prstr(g, "0123456789pqr [0]"); prstr(f, "0123456789pqr [0]"); g = "abc"; h = f; prstr(f, "0123456789pqr [0]"); prstr(h, "0123456789pqr [0]"); g.swap(f); h = f; prstr(f, "abc [1]"); prstr(h, "abc [1]"); h = g; prstr(g, "0123456789pqr [1]"); swap(f, h); prstr(f, "0123456789pqr [1]"); prstr(h, "abc [0]"); h += "def"; swap(h, h); prstr(h, "abcdef [0]"); String a(g, 10); prstr(a, "pqr [0]"); String b(g, 0, 10); prstr(b, "0123456789 [0]"); String c("abcdefghi"); prstr(c, "abcdefghi [0]"); String d("abcdefghi", 4); prstr(d, "abcd [0]"); String e(5, 'e'); prstr(e, "eeeee [0]"); e = 'e'; prstr(e, "e [0]"); e.reserve(4); prstr(e, "e [0]"); f = e; prstr(e, "e [0]"); const char *ed1, *fd1, *ed2, *fd2; ed1 = e.data(); fd1 = f.data(); compare(e.capacity(), 4); compare(f.capacity(), 1); b = ""; // create some space e += 'f'; f += 'f'; ed2 = ed1; fd2 = fd1; ed1 = e.data(); fd1 = f.data(); compare(ed1, ed2, true); compare(fd1, fd2, false); compare(e.capacity(), 4); compare(f.capacity(), 2); e += "gh"; f += "gh"; ed2 = ed1; fd2 = fd1; ed1 = e.data(); fd1 = f.data(); compare(ed1, ed2, true); compare(fd1, fd2, false); prstr(e, "efgh [0]"); prstr(f, "efgh [0]"); a = "abcdefg"; const char *ad1, *ad2, *ad3; ad1 = a.data(); ad2 = a.c_str(); compare(ad1,ad2,false); ad3 = a.c_str(); compare(ad2,ad3,true); a.reserve(8); ad1 = a.data(); compare(ad3,ad1,false); ad2 = a.c_str(); compare(ad1,ad2,true); ad3 = a.c_str(); compare(ad2,ad3,true); a = "abcdefghij"; a.reserve(1); a.erase(3,2); prstr(a, "abcfghij [0]"); compare(a.capacity(), 10); a.reserve(); a.erase(3,2); prstr(a, "abchij [0]"); compare(a.capacity(), 6); cout << "test reserve\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; { // giving capacity to a regular string without and with change in // length of data array String x = "Mary had a little lamb"; String y = x; const char* X = x.data(); const char* Y = y.data(); x.reserve(22); y.reserve(23); compare(X, x.data(), true); compare(Y, y.data(), false); prstr(x, "Mary had a little lamb [0]"); prstr(y, "Mary had a little lamb [0]"); x = String(x,0,10) + String(x,10); y = String(y,0,8) + String(y,8); x.reserve(22); y.reserve(23); prstr(x, "Mary had a little lamb [0]"); prstr(y, "Mary had a little lamb [0]"); compare(x.capacity(), 22); compare(y.capacity(),23); x.reserve(); y = x; prstr(y, "Mary had a little lamb [1]"); x.reserve(); prstr(x, "Mary had a little lamb [0]"); prstr(y, "Mary had a little lamb [0]"); y = x; x.reserve(100); prstr(x, "Mary had a little lamb [0]"); prstr(y, "Mary had a little lamb [0]"); X = x.data(); Y = y.data(); compare(X, x.c_str(), true); // shouldn't change compare(Y, y.c_str(), false); // should change prstr(x, "Mary had a little lamb [0]"); prstr(y, "Mary had a little lamb [0]"); x.reserve(100); y.reserve(100); prstr(x, "Mary had a little lamb [0]"); prstr(y, "Mary had a little lamb [0]"); compare(X, x.c_str(), false); // does change in current version compare(x.capacity(), 100); compare(y.capacity(), 100); } cout << "test append\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; e = "ex"; prstr(e, "ex [0]"); f = e; prstr(e, "ex [1]"); e.reserve(4000); prstr(e, "ex [0]"); e = 'e'; prstr(e, "e [0]"); f = e; prstr(e, "e [0]"); e.reserve(4000); prstr(e, "e [0]"); ed1 = e.data(); fd1 = f.data(); a = "0123456789"; e.append(10,'f'); f.append(10,'f'); ed2 = ed1; fd2 = fd1; ed1 = e.data(); fd1 = f.data(); compare(ed1, ed2, true); compare(fd1, fd2, false); e.append("ghi",2); f.append("gh"); ed2 = ed1; fd2 = fd1; ed1 = e.data(); fd1 = f.data(); compare(ed1, ed2, true); compare(fd1, fd2, false); e.append(a,4,2); f.append(a,4,2); ed2 = ed1; fd2 = fd1; ed1 = e.data(); fd1 = f.data(); compare(ed1, ed2, true); compare(fd1, fd2, false); e.append(a,8,String::npos); f.append(a,8,String::npos); ed2 = ed1; fd2 = fd1; ed1 = e.data(); fd1 = f.data(); compare(ed1, ed2, true); compare(fd1, fd2, false); prstr(e, "effffffffffgh4589 [0]"); prstr(f, "effffffffffgh4589 [0]"); a.resize(12,'f'); prstr(a, "0123456789ff [0]"); a.resize(6,'f'); prstr(a, "012345 [0]"); String x; compare_bool(x.empty(), true); x.append("abc"); prstr(x, "abc [0]"); x.push_back('d'); x.push_back('e'); x.push_back('f'); prstr(x, "abcdef [0]"); compare_bool(x.empty(), false); cout << "test element access\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; a = "0123456789"; b = a; prstr(a, "0123456789 [1]"); a[0] = a[8]; a[1] = a[9]; prstr(a, "8923456789 [0]"); prstr(b, "0123456789 [0]"); const String w = "0123456789"; x = w; prstr(w, "0123456789 [1]"); a[0] = w[9]; a[9] = w[0]; prstr(w, "0123456789 [1]"); prstr(a, "9923456780 [0]"); prstr(x, "0123456789 [1]"); compare((unsigned int)w[10], 0); a = "0123456789"; b = a; prstr(a, "0123456789 [1]"); a.at(0) = a.at(8); a.at(1) = a.at(9); prstr(a, "8923456789 [0]"); prstr(b, "0123456789 [0]"); x = w; prstr(w, "0123456789 [1]"); a.at(0) = w.at(9); a.at(9) = w.at(0); prstr(w, "0123456789 [1]"); prstr(a, "9923456780 [0]"); prstr(x, "0123456789 [1]"); cout << "test assign\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; a.assign(w); prstr(a, "0123456789 [2]"); a.assign(w, 5,3); prstr(a, "567 [0]"); prstr(w, "0123456789 [1]"); a.assign(w, 4,30); prstr(a, "456789 [0]"); a.assign("abcdef", 3); prstr(a, "abc [0]"); a.assign("abcdef"); prstr(a, "abcdef [0]"); a.assign("abcdef", 100); prstr(a, "abcdef [0]");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -