1 /*******************************************************
2 * Unit test for the String class
4 * test substring operations
5 ******************************************************
14 // Default contructor: empty string
18 String s1 = "abcdefghijkl";
19 String s2 = s1(3,5); // s2 = "defgh"
21 std::cout << "The substring (3,5) of " << s1 << " is " << s2 << "\n";
22 assert(s2 == "defgh");
24 // Select a substring at the end of the string
26 s2 = s1(6,6); // s2 = "ghijkl"
28 std::cout << "The substring (6,6) of " << s1 << " is " << s2 << "\n";
29 assert(s2 == "ghijkl");
31 String s3 = "abcdefghijkl";
32 String s4 = "12345678";
34 std::cout << "Replace substring (3,5) of " << s3 << " with " << s4 << " : ";
35 s3(3,5) = s4; // s3 = "abc12345678ijkl"
36 std::cout << s3 << "\n";
37 assert(s3 == "abc12345678ijkl");
39 String s5 = "abcdefghijkl";
40 String s6 = "12345678";
42 std::cout << "Insert \"" << s6 << "\" into \"" << s5 << "\" : ";
44 std::cout << s5 << "\n";
45 assert(s5 == "abc12345678defghijkl");
47 std::cout << "Remove part of \"" << s5 << "\" : ";
49 std::cout << s5 << "\n";
50 assert(s5 == "abc12345678ijkl");
52 // Select a substring out of bounds
55 String s7 = "abcdefghijkl";
56 String s8 = s7(9,5); // s7 = "jkl.."
58 std::cout << "The substring (9,5) of " << s7 << " is " << s8 << "\n";
60 catch (StringException se)
62 std::cout << "String exception: " << se.what() << "\n";
65 //assert(s2 == "defgh");