e824c6d03d3f3b728fe3297a2183a5972c51c1bc
[libacl.git] / test / string_basics.cpp
1 /*******************************************************
2  *  Unit test for the String class
3  *
4  * test basic string 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    std::cout << "The default contructor makes an empty string: \"" << s0 << "\"\n";
18    assert(~s0 == 0);
19
20    // The length of a string with the ~ operator
21    String s1("abcde");
22    int    length;
23
24    // check boolean opareations that check for empty strings
25
26    if (s0)
27    {
28       std::cout << "\"" << s0 << "\" is not empty.\n";
29    }
30    else
31    {
32       std::cout << "\"" << s0 << "\" is empty.\n";
33    }
34    if (!s0)
35    {
36       std::cout << "\"" << s0 << "\" is empty.\n";
37    }
38    else
39    {
40       std::cout << "\"" << s0 << "\" is not empty.\n";
41    }
42
43    if (s1)
44    {
45       std::cout << "\"" << s1 << "\" is not empty.\n";
46    }
47    else
48    {
49       std::cout << "\"" << s1 << "\" is empty.\n";
50    }
51    if (!s1)
52    {
53       std::cout << "\"" << s1 << "\" is empty.\n";
54    }
55    else
56    {
57       std::cout << "\"" << s1 << "\" is not empty.\n";
58    }
59
60    length = ~s1;
61    std::cout << "The length of \"" << s1 << "\" is " << length << "\n";
62    assert(length == 5);
63
64    // test character access, also outside boundaries (e.g. -1, 666)
65
66    char c;
67    c = s1[0];
68    std::cout << "First character of " << s1 << " '" << c << "'\n";
69    assert(c == 'a');
70    c = s1[3];
71    std::cout << "Fourth character of " << s1 << " '" << c << "'\n";
72    assert(c == 'd');
73
74    // Out of bounds
75    try
76    {
77       c = s1[-1];
78       std::cout << "Seventh character of " << s1 << " char(" << (int)c << ")\n";
79    }
80    catch (StringException se)
81    {
82       std::cout << "String exception: " << se.what() << "\n";
83    }
84    //assert(c == 'a');
85
86    s1[2] = 'Z';
87    std::cout << "After \"abcde\"[2] = 'Z' : " << s1 << "\n";
88    assert(s1 == "abZde");
89
90    try
91    {
92       s1[5] = 'X';
93    }
94    catch (StringException se)
95    {
96       std::cout << "String exception: " << se.what() << "\n";
97    }
98    std::cout << "After \"abcde\"[5] = 'X' : " << s1 << "\n";
99    assert(s1 == "abZde");
100
101    return 0;
102 }
103