Author: sebor
Date: Fri Feb 17 10:30:58 2006
New Revision: 378581
URL: http://svn.apache.org/viewcvs?rev=378581&view=rev
Log:
2006-02-17 Martin Sebor <[EMAIL PROTECTED]>
* alg_test.h (first_less): New helper function declaration.
(from_char): Added a third argument to check that the source array
is sorted in non-descending order.
(ilog2, ilog10): Outlined for better compilation efficiency.
(is_sorted): New helper function template.
* alg_test.cpp (first_less): Defined.
(from_char): Verified that the source array is sorted and returned
0 when it's not.
Modified:
incubator/stdcxx/trunk/tests/include/alg_test.h
incubator/stdcxx/trunk/tests/src/alg_test.cpp
Modified: incubator/stdcxx/trunk/tests/include/alg_test.h
URL:
http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/include/alg_test.h?rev=378581&r1=378580&r2=378581&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/include/alg_test.h (original)
+++ incubator/stdcxx/trunk/tests/include/alg_test.h Fri Feb 17 10:30:58 2006
@@ -127,11 +127,22 @@
_RWSTD_SIZE_T n_op_eq,
_RWSTD_SIZE_T n_op_lt);
+ // returns a pointer to the first element in the sequence whose value
+ // is less than the value of the immediately preceding element, or 0
+ // when no such element exists
+ static const X*
+ first_less (const X*, _RWSTD_SIZE_T);
+
static void reset_totals ();
// construct an array of objects of type X each initialized
// from the corresponding element of the character array
- static X* from_char (const char*, _RWSTD_SIZE_T = _RWSTD_SIZE_MAX);
+ // when the last argument is true and the character array
+ // is not sorted in ascending order the function fails by
+ // returning 0
+ static X*
+ from_char (const char*, _RWSTD_SIZE_T = _RWSTD_SIZE_MAX,
+ bool = false);
// returns -1 when less, 0 when same, or +1 when the array
// of X objects is greater than the character string
@@ -156,28 +167,30 @@
// wrapper around a (possibly) extern "C" int rand()
// extern "C++"
-_TEST_EXPORT int gen_rnd ();
+_TEST_EXPORT int gen_rnd ();
// computes an integral log2
-inline unsigned ilog2 (unsigned long n)
-{
- unsigned result = 0;
- while (n >>= 1)
- ++result;
- return result;
-}
-
+_TEST_EXPORT unsigned ilog2 (_RWSTD_SIZE_T);
// computes an integral log10
-inline unsigned ilog10 (unsigned long n)
+_TEST_EXPORT unsigned ilog10 (_RWSTD_SIZE_T);
+
+
+template <class InputIterator, class Predicate>
+inline bool
+is_sorted (InputIterator first, InputIterator last, Predicate pred)
{
- unsigned result = 0;
- while (n /= 10)
- ++result;
- return result;
-}
+ if (first == last)
+ return true;
+ for (InputIterator prev (first); ++first != last; prev = first) {
+ if (pred (*first, *prev))
+ return false;
+ }
+
+ return true;
+}
// returns true iff a sequence of (not necessarily unique) values
// is sorted in an ascending order
Modified: incubator/stdcxx/trunk/tests/src/alg_test.cpp
URL:
http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/src/alg_test.cpp?rev=378581&r1=378580&r2=378581&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/src/alg_test.cpp (original)
+++ incubator/stdcxx/trunk/tests/src/alg_test.cpp Fri Feb 17 10:30:58 2006
@@ -306,6 +306,22 @@
}
+/* static */ const X*
+X::first_less (const X *xarray, size_t nelems)
+{
+ size_t inx = nelems;
+
+ if (1 < nelems) {
+ for (inx = 1; inx != nelems; ++inx) {
+ if (xarray [inx] < xarray [inx - 1])
+ break;
+ }
+ }
+
+ return inx < nelems ? xarray + inx : 0;
+}
+
+
/* static */ void
X::reset_totals ()
{
@@ -333,7 +349,7 @@
/* static */ X*
-X::from_char (const char *str, size_t len /* = -1 */)
+X::from_char (const char *str, size_t len /* = -1 */, bool sorted /* = false
*/)
{
// handle null pointers
if (!str)
@@ -343,6 +359,15 @@
if (size_t (-1) == len)
len = strlen (str);
+ if (sorted) {
+ // verify that the sequence is sorted
+ for (size_t i = 1; i < len; ++i) {
+ if (str [i] < str [i - 1]) {
+ return 0;
+ }
+ }
+ }
+
// set the global pointer to point to the beginning of `str'
xinit_begin = str;
@@ -545,6 +570,30 @@
_TEST_EXPORT int gen_rnd ()
{
return rand ();
+}
+
+
+_TEST_EXPORT unsigned
+ilog2 (size_t n)
+{
+ unsigned result = 0;
+
+ while (n >>= 1)
+ ++result;
+
+ return result;
+}
+
+
+_TEST_EXPORT unsigned
+ilog10 (size_t n)
+{
+ unsigned result = 0;
+
+ while (n /= 10)
+ ++result;
+
+ return result;
}