Documentation and test for String::index() and String::rindex()
[libacl.git] / test / string_search.cpp
diff --git a/test/string_search.cpp b/test/string_search.cpp
new file mode 100644 (file)
index 0000000..c7ffad9
--- /dev/null
@@ -0,0 +1,71 @@
+/*******************************************************
+ *  Unit test for the String class
+ *
+ * test search operations
+ ******************************************************
+ *
+ */
+
+#include "String.h"
+#include <assert.h>
+
+int main()
+{
+   int i;
+
+   // Search for characters
+
+   String s1("abcdabcd");
+
+   i = s1.index('c');
+   std::cout << "First occurance of 'c' in " << s1 << " is " << i << "\n";
+   assert(i == 2);
+
+   i = s1.index('f');
+   std::cout << "First occurance of 'f' in " << s1 << " is " << i << "\n";
+   assert(i == -1);
+
+   i = s1.rindex('c');
+   std::cout << "Last occurance of 'c' in " << s1 << " is " << i << "\n";
+   assert(i == 6);
+
+   i = s1.rindex('f');
+   std::cout << "Last occurance of 'f' in " << s1 << " is " << i << "\n";
+   assert(i == -1);
+
+   //   Search for strings
+
+   String s2("abcdef-abcdefghi");
+
+   i = s2.index("def");
+   std::cout << "First occurance of \"def\" in " << s2 << " is " << i << "\n";
+   assert(i == 3);
+
+   i = s2.index("xyz");
+   std::cout << "First occurance of \"xyz\" in " << s2 << " is " << i << "\n";
+   assert(i == -1);
+
+   i = s2.rindex("def");
+   std::cout << "Last occurance of \"def\" in " << s2 << " is " << i << "\n";
+   assert(i == 10);
+
+   i = s2.rindex("xyz");
+   std::cout << "Last occurance of \"xyz\" in " << s2 << " is " << i << "\n";
+   assert(i == -1);
+
+   //  check matches at the start of a string
+
+   String s3("abcdefghi");
+   String s4("ab");
+
+   i = s3.index(s4);
+   std::cout << "First occurance of \"" << s4 << "\" in " << s3 << " is " << i << "\n";
+   assert(i == 0);
+
+   i = s3.rindex(s4);
+   std::cout << "Last occurance of \"" << s4 << "\" in " << s3 << " is " << i << "\n";
+   assert(i == 0);
+
+   return 0;
+}
+