You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stdcxx.apache.org by se...@apache.org on 2006/03/10 03:28:07 UTC
svn commit: r384680 - in /incubator/stdcxx/trunk/tests: include/rw_char.h
src/char.cpp
Author: sebor
Date: Thu Mar 9 18:27:59 2006
New Revision: 384680
URL: http://svn.apache.org/viewcvs?rev=384680&view=rev
Log:
2006-03-09 Martin Sebor <se...@roguewave.com>
* rw_char.h (UserTraits<UserChar>): Declared ctor, dtor and assignment
private to detect unwarranted assumptions.
(MemFun, n_calls_): New member class and array of counters to keep
track of member function calls.
(assign, eq, lt, not_eof, to_char_type, to_int_type, eq_int_type,
eof): Outlined UserTraits members.
* char.cpp (assign, eq, lt, not_eof, to_char_type, to_int_type,
eq_int_type, eof): Defined out of line.
(copy, move): Corrected logic errors.
Modified:
incubator/stdcxx/trunk/tests/include/rw_char.h
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=384680&r1=384679&r2=384680&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/include/rw_char.h (original)
+++ incubator/stdcxx/trunk/tests/include/rw_char.h Thu Mar 9 18:27:59 2006
@@ -193,6 +193,26 @@
_RWSTD_SPECIALIZED_CLASS
struct _TEST_EXPORT UserTraits<UserChar> // user-defined character traits
{
+private:
+
+ // not defined to detect bad assumptions made by the library
+ UserTraits ();
+ ~UserTraits ();
+ void operator= (UserTraits&);
+
+public:
+
+ struct MemFun {
+ enum {
+ assign, eq, lt, compare, length, find, copy, move,
+ assign2, not_eof, to_char_type, to_int_type, eq_int_type,
+ eof,
+ n_funs
+ };
+ };
+
+ static _RWSTD_SIZE_T n_calls_ [];
+
typedef UserChar char_type;
typedef UserInt int_type;
@@ -204,24 +224,17 @@
// accesses to the char_type::f member may trigger a SIGBUS
// on some architectures (e.g., PA or SPARC) if the member
// isn't appropriately aligned
- static void assign (char_type &c1, const char_type &c2) {
- c1.f = c2.f;
- c1.c = c2.c;
- }
-
- static bool eq (const char_type &c1, const char_type &c2) {
- return c1.f == c2.f && c1.c == c2.c;
- }
-
- static bool lt (const char_type &c1, const char_type &c2) {
- return c1.f < c2.f || c1.f == c2.f && c1.c < c2.c;
- }
+
+ static void assign (char_type&, const char_type&);
+
+ static bool eq (const char_type&, const char_type&);
+
+ static bool lt (const char_type&, const char_type&);
static int
compare (const char_type*, const char_type*, _RWSTD_SIZE_T);
- static _RWSTD_SIZE_T
- length (const char_type*);
+ static _RWSTD_SIZE_T length (const char_type*);
static const char_type*
find (const char_type*, _RWSTD_SIZE_T, const char_type&);
@@ -235,30 +248,15 @@
static char_type*
assign (char_type*, _RWSTD_SIZE_T, char_type);
- static int_type
- not_eof (const int_type &i) {
- if (eq_int_type (i, int_type::eof ())) {
- const char_type c = { 0, 0 };
- return int_type::from_char (c);
- }
- return i;
- }
-
- static char_type to_char_type (const int_type &i) {
- return i.to_char ();
- }
+ static int_type not_eof (const int_type&);
+
+ static char_type to_char_type (const int_type&);
- static int_type to_int_type (const char_type &c) {
- return int_type::from_char (c);
- }
-
- static bool eq_int_type (const int_type &i1, const int_type &i2) {
- return i1.equal (i2);
- }
-
- static int_type eof () {
- return int_type::eof ();
- }
+ static int_type to_int_type (const char_type&);
+
+ static bool eq_int_type (const int_type&, const int_type&);
+
+ static int_type eof ();
};
Modified: incubator/stdcxx/trunk/tests/src/char.cpp
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/src/char.cpp?rev=384680&r1=384679&r2=384680&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/src/char.cpp (original)
+++ incubator/stdcxx/trunk/tests/src/char.cpp Thu Mar 9 18:27:59 2006
@@ -32,14 +32,56 @@
#include <string.h> // for memcpy()
+size_t
+UserTraits<UserChar>::
+n_calls_ [UserTraits<UserChar>::MemFun::n_funs];
+
+
+void
+UserTraits<UserChar>::
+assign (char_type &c1, const char_type &c2)
+{
+ ++n_calls_ [MemFun::assign];
+
+ c1.f = c2.f;
+ c1.c = c2.c;
+}
+
+
+bool
+UserTraits<UserChar>::
+eq (const char_type &c1, const char_type &c2)
+{
+ ++n_calls_ [MemFun::eq];
+
+ return c1.f == c2.f && c1.c == c2.c;
+}
+
+
+bool
+UserTraits<UserChar>::
+lt (const char_type &c1, const char_type &c2)
+{
+ ++n_calls_ [MemFun::lt];
+
+ return c1.f < c2.f || c1.f == c2.f && c1.c < c2.c;
+}
+
+
int UserTraits<UserChar>::
compare (const char_type *s1, const char_type *s2, size_t n)
{
RW_ASSERT (0 == n || s1 && s2);
+ ++n_calls_ [MemFun::compare];
+
for (size_t i = 0; i != n; ++i) {
- if (!eq (s1[i], s2[i])) {
- return lt (s1[i], s2[i]) ? -1 : 1;
+ if (s1 [i].f != s2 [i].f || s1 [i].c != s2 [i].c) {
+ if ( s1 [i].f < s2 [i].f
+ || s1 [i].f == s2 [i].f && s1 [i].c < s2 [i].c)
+ return -1;
+
+ return 1;
}
}
@@ -52,8 +94,12 @@
{
RW_ASSERT (0 != s);
+ ++n_calls_ [MemFun::length];
+
size_t len = 0;
- for (; !eq (*s++, char_type::eos ()); ++len);
+
+ for (; s [len].f || s [len].c; ++len);
+
return len;
}
@@ -64,9 +110,14 @@
{
RW_ASSERT (0 == n || s);
- for (; n-- && !eq (*s, c); ++s);
+ ++n_calls_ [MemFun::find];
- return eq (*s, c) ? s : 0;
+ for (; n--; ++s) {
+ if (s->f == c.f && s->c == c.c)
+ return s;
+ }
+
+ return 0;
}
@@ -74,7 +125,11 @@
UserTraits<UserChar>::
copy (char_type *dst, const char_type *src, size_t n)
{
- for (; n--; *dst++ = *src++);
+ RW_ASSERT (0 == n || dst && src);
+
+ ++n_calls_ [MemFun::copy];
+
+ for (size_t i = 0; i != n; dst [i] = src [i]);
return dst;
}
@@ -82,19 +137,15 @@
UserTraits<UserChar>::char_type*
UserTraits<UserChar>::
-move (char_type *s1, const char_type *s2, size_t n)
+move (char_type *dst, const char_type *src, size_t n)
{
- RW_ASSERT (0 == n || s1 && s2);
+ RW_ASSERT (0 == n || dst && src);
- if (s1 < s2)
- copy (s1, s2, n);
- else if (s2 < s1) {
- s1 += n;
- s2 += n;
- for (size_t i = 0; i != n; ++i)
- assign (*--s1, *--s2);
- }
- return s1;
+ ++n_calls_ [MemFun::move];
+
+ for (; n--; dst [n] = src [n]);
+
+ return dst;
}
@@ -104,9 +155,67 @@
{
RW_ASSERT (0 == n || s);
- for (; n--; assign (s [n], c));
+ ++n_calls_ [MemFun::assign2];
+
+ for (; n--; s [n] = c);
return s;
+}
+
+
+UserTraits<UserChar>::int_type
+UserTraits<UserChar>::
+not_eof (const int_type &i)
+{
+ ++n_calls_ [MemFun::not_eof];
+
+ if (i.equal (int_type::eof ())) {
+ const char_type c = { 0, 0 };
+
+ return int_type::from_char (c);
+ }
+
+ return i;
+}
+
+
+UserTraits<UserChar>::char_type
+UserTraits<UserChar>::
+to_char_type (const int_type &i)
+{
+ ++n_calls_ [MemFun::to_char_type];
+
+ return i.to_char ();
+}
+
+
+UserTraits<UserChar>::int_type
+UserTraits<UserChar>::
+to_int_type (const char_type &c)
+{
+ ++n_calls_ [MemFun::to_int_type];
+
+ return int_type::from_char (c);
+}
+
+
+bool
+UserTraits<UserChar>::
+eq_int_type (const int_type &i1, const int_type &i2)
+{
+ ++n_calls_ [MemFun::eq_int_type];
+
+ return i1.equal (i2);
+}
+
+
+UserTraits<UserChar>::int_type
+UserTraits<UserChar>::
+eof ()
+{
+ ++n_calls_ [MemFun::eof];
+
+ return int_type::eof ();
}