stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject svn commit: r387628 - in /incubator/stdcxx/trunk/tests: include/rw_char.h self/0.char.cpp src/char.cpp
Date Tue, 21 Mar 2006 21:38:53 GMT
Author: sebor
Date: Tue Mar 21 13:38:52 2006
New Revision: 387628

URL: http://svn.apache.org/viewcvs?rev=387628&view=rev
Log:
2006-03-21  Martin Sebor  <sebor@roguewave.com>

	* rw_char.h (rw_match): Declared and documented new helpers.
	* src/char.cpp (rw_match): Implemented new helpers.
	* test/char.cpp (test_rw_match): Exercised new helpers.

Modified:
    incubator/stdcxx/trunk/tests/include/rw_char.h
    incubator/stdcxx/trunk/tests/self/0.char.cpp
    incubator/stdcxx/trunk/tests/src/char.cpp

Modified: incubator/stdcxx/trunk/tests/include/rw_char.h
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/include/rw_char.h?rev=387628&r1=387627&r2=387628&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/include/rw_char.h (original)
+++ incubator/stdcxx/trunk/tests/include/rw_char.h Tue Mar 21 13:38:52 2006
@@ -281,6 +281,17 @@
                  const char*   /* src */,
                  _RWSTD_SIZE_T /* len */ = _RWSTD_SIZE_MAX);
 
+// rw_match() compares up to len successive elements of s1 and s2 for
+// equality until it finds a pair that do not compare equal or until
+// len comparisons has been made
+// if (len == SIZE_MAX) is true elements are compared until the first
+// mismatch is found or until the NUL character is encountered
+// returns the number of matching elements
+_TEST_EXPORT
+_RWSTD_SIZE_T rw_match (const char*   /* s1  */,
+                        const char*   /* s2  */,
+                        _RWSTD_SIZE_T /* len */ = _RWSTD_SIZE_MAX);
+
 #ifndef _RWSTD_WCHAR_T
 
 _TEST_EXPORT
@@ -289,6 +300,10 @@
 _TEST_EXPORT
 char* rw_narrow (char*, const wchar_t*, _RWSTD_SIZE_T = _RWSTD_SIZE_MAX);
 
+_TEST_EXPORT
+_RWSTD_SIZE_T rw_match (const char*, const wchar_t*,
+                        _RWSTD_SIZE_T = _RWSTD_SIZE_MAX);
+
 #endif   // _RWSTD_WCHAR_T
 
 _TEST_EXPORT
@@ -296,5 +311,9 @@
 
 _TEST_EXPORT
 char* rw_narrow (char*, const UserChar*, _RWSTD_SIZE_T = _RWSTD_SIZE_MAX);
+
+_TEST_EXPORT
+_RWSTD_SIZE_T rw_match (const char*, const UserChar*,
+                        _RWSTD_SIZE_T = _RWSTD_SIZE_MAX);
 
 #endif   // RW_CHAR_INCLUDED

Modified: incubator/stdcxx/trunk/tests/self/0.char.cpp
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/self/0.char.cpp?rev=387628&r1=387627&r2=387628&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/self/0.char.cpp (original)
+++ incubator/stdcxx/trunk/tests/self/0.char.cpp Tue Mar 21 13:38:52 2006
@@ -375,7 +375,7 @@
 /***********************************************************************/
 
 static void
-test_widen ()
+test_rw_widen ()
 {
     //////////////////////////////////////////////////////////////////
     rw_info (0, 0, 0, "rw_widen(char*, const char*, size_t)");
@@ -409,13 +409,12 @@
                "rw_widen(char*, %{#s}, %zu) == %{#*s}, got %{#*s}",
                0, sizeof cdst, int (sizeof cdst), null,
                int (sizeof cdst), cdst);
-               
-
-#ifndef _RWSTD_NO_WCHAR_T
 
     //////////////////////////////////////////////////////////////////
     rw_info (0, 0, 0, "rw_widen(wchar_t*, const char*, size_t)");
 
+#ifndef _RWSTD_NO_WCHAR_T
+
     static const wchar_t wsrc [] = L"abcdefgh";
     static const wchar_t wnull [nsrc + 1] = L"";
     wchar_t              wdst [nsrc + 1];
@@ -447,6 +446,10 @@
                0, sizeof wdst, int (sizeof wdst / sizeof *wdst), wnull,
                int (sizeof wdst / sizeof *wdst), wdst);
 
+#else   // if defined (_RWSTD_NO_WCHAR_T)
+
+    rw_note (0, 0, 0, "_RWSTD_NO_WCHAR_T #defined, wchar_t test disabled");
+
 #endif   // _RWSTD_NO_WCHAR_T
 
     //////////////////////////////////////////////////////////////////
@@ -488,7 +491,7 @@
 /***********************************************************************/
 
 static void
-test_narrow ()
+test_rw_narrow ()
 {
     //////////////////////////////////////////////////////////////////
     rw_info (0, 0, 0, "rw_narrow(char*, const char*, size_t)");
@@ -523,12 +526,11 @@
                0, sizeof cdst, int (sizeof cdst), null,
                int (sizeof cdst), cdst);
                
-
-#ifndef _RWSTD_NO_WCHAR_T
-
     //////////////////////////////////////////////////////////////////
     rw_info (0, 0, 0, "rw_narrow(wchar_t*, const wchar_t*, size_t)");
 
+#ifndef _RWSTD_NO_WCHAR_T
+
     static const wchar_t wsrc [] = L"abcdefgh";
 
     for (size_t i = 0; i != nsrc + 1; ++i) {
@@ -557,6 +559,10 @@
                0, sizeof cdst, int (sizeof null), null,
                int (sizeof cdst), cdst);
 
+#else   // if defined (_RWSTD_NO_WCHAR_T)
+
+    rw_note (0, 0, 0, "_RWSTD_NO_WCHAR_T #defined, wchar_t test disabled");
+
 #endif   // _RWSTD_NO_WCHAR_T
 
     //////////////////////////////////////////////////////////////////
@@ -595,12 +601,193 @@
 
 /***********************************************************************/
 
+static size_t
+length (const char *s)
+{
+    return strlen (s);
+}
+
+
+static size_t
+length (const wchar_t *ws)
+{
+    size_t len = 0;
+    if (ws)
+        for (len = 0; ws [len]; ++len);
+    return len;
+}
+
+
+static size_t
+length (const UserChar *us)
+{
+    size_t len = 0;
+    if (us)
+        for (len = 0; us [len].f || us [len].c; ++len);
+    return len;
+}
+
+
+static const UserChar*
+make_user_string (const char *s, size_t len)
+{
+    static UserChar usrbuf [32];
+    memset (usrbuf, 0, sizeof usrbuf);
+
+    if (s) {
+        for (size_t i = 0; i != len; ++i) {
+
+            typedef unsigned char UChar;
+
+            usrbuf [i].f = 0;
+            usrbuf [i].c = UChar (s [i]);
+        }
+        return usrbuf;
+    }
+
+    return 0;
+}
+
+
+static void
+test_rw_match ()
+{
+    //////////////////////////////////////////////////////////////////
+    rw_info (0, 0, 0, "rw_match(char*, const char*, size_t)");
+
+    const size_t size_max = _RWSTD_SIZE_MAX;
+    size_t result;
+
+#undef TEST
+#define TEST(s1, s2, len, expect)                                         \
+  result = rw_match ((const char*)s1, (const char*)s2, size_t (len));     \
+  rw_assert (expect == result,                                            \
+             0, __LINE__,                                                 \
+             "rw_match(%{#*s}, %{#*s}, %zu) == %zu, got %zu",             \
+             int (size_max == len ? length ((const char*)s1) : len), s1,  \
+             int (size_max == len ? length ((const char*)s2) : len), s2,  \
+             len, expect, result)
+
+    TEST (0,      0,        -1, 0);
+    TEST ("",     0,        -1, 0);
+    TEST (0,      "",       -1, 0);
+    TEST ("",     "",       -1, 0);
+
+    // when invoked with NULL as the first string returns
+    // the length of the second string (if non-NULL)
+    TEST (0,      "a",      -1, 1);
+    TEST (0,      "ab",     -1, 2);
+    TEST (0,      "abc",    -1, 3);
+
+    // same as above but with the arguments reversed
+    TEST ("a",    0,        -1, 1);
+    TEST ("ab",   0,        -1, 2);
+    TEST ("abc",  0,        -1, 3);
+
+    TEST ("",     "a",      -1, 0);
+    TEST ("a",    "",       -1, 0);
+    TEST ("a",    "a",      -1, 1);
+
+    TEST ("a\0bc", "a\0bd", -1, 1);
+    TEST ("a\0bc", "a\0bd",  0, 0);
+    TEST ("a\0bc", "a\0bd",  1, 1);
+    TEST ("a\0bc", "a\0bd",  2, 2);
+    TEST ("a\0bc", "a\0bd",  3, 3);
+    TEST ("a\0bc", "a\0bd",  4, 3);
+
+    //////////////////////////////////////////////////////////////////
+    rw_info (0, 0, 0, "rw_match(char*, const wchar_t*, size_t)");
+
+#ifndef _RWSTD_NO_WCHAR_T
+
+#undef TEST
+#define TEST(s1, s2, len, expect)                                            \
+  result = rw_match ((const char*)s1, (const wchar_t*)s2, size_t (len));     \
+  rw_assert (expect == result,                                               \
+             0, __LINE__,                                                    \
+             "rw_match(%{#*s}, L%{#*ls}, %zu) == %zu, got %zu",              \
+             int (size_max == len ? length ((const char*)s1) : len), s1,     \
+             int (size_max == len ? length ((const wchar_t*)s2) : len), s2,  \
+             len, expect, result)
+
+    TEST (0,       0,        -1, 0);
+    TEST ("",      0,        -1, 0);
+    TEST (0,       L"",      -1, 0);
+    TEST ("",      L"",      -1, 0);
+
+    TEST (0,       L"a",     -1, 1);
+    TEST (0,       L"ab",    -1, 2);
+    TEST (0,       L"abc",   -1, 3);
+
+    TEST ("a",     0,        -1, 1);
+    TEST ("ab",    0,        -1, 2);
+    TEST ("abc",   0,        -1, 3);
+
+    TEST ("",      L"a",     -1, 0);
+    TEST ("a",     L"",      -1, 0);
+    TEST ("a",     L"a",     -1, 1);
+
+    TEST ("a\0bc", L"a\0bd", -1, 1);
+    TEST ("a\0bc", L"a\0bd",  0, 0);
+    TEST ("a\0bc", L"a\0bd",  1, 1);
+    TEST ("a\0bc", L"a\0bd",  2, 2);
+    TEST ("a\0bc", L"a\0bd",  3, 3);
+    TEST ("a\0bc", L"a\0bd",  4, 3);
+
+#else   // if defined (_RWSTD_NO_WCHAR_T)
+
+    rw_note (0, 0, 0, "_RWSTD_NO_WCHAR_T #defined, wchar_t test disabled");
+
+#endif   // _RWSTD_NO_WCHAR_T
+
+    //////////////////////////////////////////////////////////////////
+    rw_info (0, 0, 0, "rw_match(char*, const UserChar*, size_t)");
+
+#undef TEST
+#define TEST(s1, s2, len, expect)                                            \
+  result = rw_match ((const char*)s1,                                        \
+                     make_user_string (s2, sizeof (s2)), size_t (len));      \
+  rw_assert (expect == result,                                               \
+             0, __LINE__,                                                    \
+             "rw_match(%{#*s}, %{#*s}, %zu) == %zu, got %zu",                \
+             int (size_max == len ? length ((const char*)s1) : len), s1,     \
+             int (size_max == len ? length ((const char*)s2) : len), s2,     \
+             len, expect, result)
+
+    TEST (0,       0,       -1, 0);
+    TEST ("",      0,       -1, 0);
+    TEST (0,       "",      -1, 0);
+    TEST ("",      "",      -1, 0);
+
+    TEST (0,       "a",     -1, 1);
+    TEST (0,       "ab",    -1, 2);
+    TEST (0,       "abc",   -1, 3);
+
+    TEST ("a",     0,       -1, 1);
+    TEST ("ab",    0,       -1, 2);
+    TEST ("abc",   0,       -1, 3);
+
+    TEST ("",      "a",     -1, 0);
+    TEST ("a",     "",      -1, 0);
+    TEST ("a",     "a",     -1, 1);
+
+    TEST ("a\0bc", "a\0bd", -1, 1);
+    TEST ("a\0bc", "a\0bd",  0, 0);
+    TEST ("a\0bc", "a\0bd",  1, 1);
+    TEST ("a\0bc", "a\0bd",  2, 2);
+    TEST ("a\0bc", "a\0bd",  3, 3);
+    TEST ("a\0bc", "a\0bd",  4, 3);
+}
+
+/***********************************************************************/
+
 static int no_user_traits;
 static int no_user_traits_char;
 static int no_user_traits_wchar_t;
 static int no_user_traits_user_char;
 static int no_rw_widen;
 static int no_rw_narrow;
+static int no_rw_match;
 
 
 static int
@@ -632,19 +819,16 @@
         }
     }
 
-    if (no_rw_widen) {
-        rw_note (0, 0, 0, "rw_widen() tests disabled");
-    }
-    else {
-        test_widen ();
-    }
-
-    if (no_rw_narrow) {
-        rw_note (0, 0, 0, "rw_narrow() tests disabled");
-    }
-    else {
-        test_narrow ();
-    }
+#undef TEST
+#define TEST(fun)                                               \
+        if (no_ ## fun)                                         \
+            rw_note (0, 0, 0, "%s() tests disabled", #fun);     \
+        else                                                    \
+            test_ ## fun ()
+
+    TEST (rw_widen);
+    TEST (rw_narrow);
+    TEST (rw_match);
 
     return 0;
 }
@@ -662,12 +846,14 @@
                     "|-no-UserTraits<wchar_t># "
                     "|-no-UserTraits<UserChar># "
                     "|-no-rw_widen# "
-                    "|-no-rw_narrow#",
+                    "|-no-rw_narrow# "
+                    "|-no-rw_macth#",
                     &no_user_traits,
                     &no_user_traits_char,
                     &no_user_traits_wchar_t,
                     &no_user_traits_user_char,
                     &no_rw_widen,
                     &no_rw_narrow,
+                    &no_rw_match,
                     0);
 }

Modified: incubator/stdcxx/trunk/tests/src/char.cpp
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/src/char.cpp?rev=387628&r1=387627&r2=387628&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/src/char.cpp (original)
+++ incubator/stdcxx/trunk/tests/src/char.cpp Tue Mar 21 13:38:52 2006
@@ -255,12 +255,36 @@
 
 
 _TEST_EXPORT
-char* rw_narrow (char *dst, const char *src, size_t len /* = SIZE_MAX */)
+char*
+rw_narrow (char *dst, const char *src, size_t len /* = SIZE_MAX */)
 {
     return rw_widen (dst, src, len);
 }
 
 
+_TEST_EXPORT
+size_t
+rw_match (const char *s1, const char *s2, size_t len /* = SIZE_MAX */)
+{
+    if (0 == s1) {
+        // return the length of s2 if non-null
+        return s2 ? strlen (s2) : 0;
+    }
+
+    if (0 == s2)
+        return strlen (s1);
+
+    size_t n = 0;
+
+    for ( ; n != len && s1 [n] == s2 [n]; ++n) {
+        if (_RWSTD_SIZE_MAX == len && '\0' == s1 [n])
+            break;
+    }
+
+    return n;
+}
+
+
 #ifndef _RWSTD_WCHAR_T
 
 _TEST_EXPORT
@@ -337,6 +361,36 @@
     return dst;
 }
 
+
+_TEST_EXPORT
+size_t
+rw_match (const char *s1, const wchar_t *s2, size_t len /* = SIZE_MAX */)
+{
+    if (0 == s1) {
+        if (s2) {
+            // return the length of s2
+            for (len = 0; s2 [len]; ++len);
+            return len;
+        }
+
+        return 0;
+    }
+
+    if (0 == s2)
+        return strlen (s1);
+
+    typedef unsigned char UChar;
+
+    size_t n = 0;
+
+    for ( ; n != len && UChar (s1 [n]) == unsigned (s2 [n]); ++n) {
+        if (_RWSTD_SIZE_MAX == len && '\0' == s1 [n])
+            break;
+    }
+
+    return n;
+}
+
 #endif   // _RWSTD_WCHAR_T
 
 
@@ -413,4 +467,34 @@
     }
 
     return dst;
+}
+
+
+_TEST_EXPORT
+size_t
+rw_match (const char *s1, const UserChar *s2, size_t len /* = SIZE_MAX */)
+{
+    if (0 == s1) {
+        if (s2) {
+            // return the length of s2
+            for (len = 0; s2 [len].f || s2 [len].c; ++len);
+            return len;
+        }
+
+        return 0;
+    }
+
+    if (0 == s2)
+        return strlen (s1);
+
+    typedef unsigned char UChar;
+
+    size_t n = 0;
+
+    for ( ; n != len && UChar (s1 [n]) == s2 [n].c; ++n) {
+        if (_RWSTD_SIZE_MAX == len && '\0' == s1 [n])
+            break;
+    }
+
+    return n;
 }



Mime
View raw message