Fix: substring throws an exception when selecting a substring at the end
[libacl.git] / test / string_substring.cpp
1 /*******************************************************
2  *  Unit test for the String class
3  *
4  * test substring operations
5  ******************************************************
6  *
7  */
8
9 #include "String.h"
10 #include <assert.h>
11
12 int main()
13 {
14    // Default contructor: empty string
15    String s0;
16
17    // Select a substring
18    String s1 = "abcdefghijkl";
19    String s2 = s1(3,5);         // s2 = "defgh"
20
21    std::cout << "The substring (3,5) of " << s1 << " is " << s2 << "\n";
22    assert(s2 == "defgh");
23
24    // Select a substring at the end of the string
25    s1 = "abcdefghijkl";
26    s2 = s1(6,6);         // s2 = "ghijkl"
27
28    std::cout << "The substring (6,6) of " << s1 << " is " << s2 << "\n";
29    assert(s2 == "ghijkl");
30
31    String s3 = "abcdefghijkl";
32    String s4 = "12345678";
33
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");
38
39    String s5 = "abcdefghijkl";
40    String s6 = "12345678";
41
42    std::cout << "Insert \"" << s6 << "\" into \"" << s5 << "\" : ";
43    s5(3, 0) = s6;
44    std::cout << s5 << "\n";
45    assert(s5 == "abc12345678defghijkl");
46
47    std::cout << "Remove part of \"" << s5 << "\" : ";
48    s5(11,5) = "";
49    std::cout << s5 << "\n";
50    assert(s5 == "abc12345678ijkl");
51
52    // Select a substring out of bounds
53    try
54    {
55       String s7 = "abcdefghijkl";
56       String s8 = s7(9,5);         // s7 = "jkl.."
57
58       std::cout << "The substring (9,5) of " << s7 << " is " << s8 << "\n";
59    }
60    catch (StringException se)
61    {
62       std::cout << "String exception: " << se.what() << "\n";
63    }
64
65    //assert(s2 == "defgh");
66
67    return 0;
68 }
69