Boost logo

Boost-Commit :

From: zeux_at_[hidden]
Date: 2007-06-04 15:27:34


Author: zeux
Date: 2007-06-04 15:27:34 EDT (Mon, 04 Jun 2007)
New Revision: 4437
URL: http://svn.boost.org/trac/boost/changeset/4437

Log:
String conversion tests, found a bug in gmp implementation's wstr() and fixed it.

Added:
   sandbox/SOC/2007/bigint/libs/bigint/test/string_conversion.cpp
Text files modified:
   sandbox/SOC/2007/bigint/boost/bigint/bigint_gmp.hpp | 2 +-
   sandbox/SOC/2007/bigint/libs/bigint/test/Jamfile.v2 | 1 +
   sandbox/SOC/2007/bigint/libs/bigint/test/number_ctors.cpp | 2 +-
   sandbox/SOC/2007/bigint/libs/bigint/todo.txt | 13 ++++++++-----
   4 files changed, 11 insertions(+), 7 deletions(-)

Modified: sandbox/SOC/2007/bigint/boost/bigint/bigint_gmp.hpp
==============================================================================
--- sandbox/SOC/2007/bigint/boost/bigint/bigint_gmp.hpp (original)
+++ sandbox/SOC/2007/bigint/boost/bigint/bigint_gmp.hpp 2007-06-04 15:27:34 EDT (Mon, 04 Jun 2007)
@@ -294,7 +294,7 @@
                         scoped_array<char> s(new char[s_size + 1]);
                         mpz_get_str(s.get(), base, data);
                         
- std::wstring result(s.get(), s.get() + s_size);
+ std::wstring result(s.get(), s.get() + strlen(s.get()));
                         return result;
                 }
                 

Modified: sandbox/SOC/2007/bigint/libs/bigint/test/Jamfile.v2
==============================================================================
--- sandbox/SOC/2007/bigint/libs/bigint/test/Jamfile.v2 (original)
+++ sandbox/SOC/2007/bigint/libs/bigint/test/Jamfile.v2 2007-06-04 15:27:34 EDT (Mon, 04 Jun 2007)
@@ -11,6 +11,7 @@
    :
    [ run bigint_simple_test.cpp ]
    [ run number_ctors.cpp ]
+ [ run string_conversion.cpp ]
    ;
 }
 

Modified: sandbox/SOC/2007/bigint/libs/bigint/test/number_ctors.cpp
==============================================================================
--- sandbox/SOC/2007/bigint/libs/bigint/test/number_ctors.cpp (original)
+++ sandbox/SOC/2007/bigint/libs/bigint/test/number_ctors.cpp 2007-06-04 15:27:34 EDT (Mon, 04 Jun 2007)
@@ -136,7 +136,7 @@
         }
 }
 
-int test_main( int argc, char* argv[] )
+int test_main(int argc, char* argv[])
 {
   test<boost::detail::bigint_gmp_implementation>();
 

Added: sandbox/SOC/2007/bigint/libs/bigint/test/string_conversion.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/bigint/libs/bigint/test/string_conversion.cpp 2007-06-04 15:27:34 EDT (Mon, 04 Jun 2007)
@@ -0,0 +1,117 @@
+/* Boost number_ctors.cpp test file
+ *
+ * Copyright 2007 Arseny Kapoulkine
+ *
+ * Distributed under the Boost Software License, Version 1.0.
+ * (See accompanying file LICENSE_1_0.txt or
+ * copy at http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#define BOOST_DISABLE_WIN32
+
+#include <boost/test/included/test_exec_monitor.hpp>
+
+#include <boost/bigint/bigint.hpp>
+
+#include <sstream>
+
+#include <iostream>
+
+#pragma comment(lib, "libgmp-3.lib")
+
+// This macro is not quite good, but - it's ok for our needs
+#define ARRAY_SIZE(array) sizeof(array) / sizeof(array[0])
+
+template <typename I> void test()
+{
+ typedef boost::bigint_base<I> number;
+
+ struct test_entry
+ {
+ int base; // negative base means that input is unusual, therefore backwards translation will fail
+ const char* lhs;
+ int value;
+ const char* rhs;
+ };
+
+ test_entry entries[] =
+ {
+ // correct base 2
+ {2, "0", 0, 0},
+ {2, "101011010", 346, 0},
+ {2, "101110000100010010110100001110000100110010100001000100110011", 0, "829870984705741107"},
+ {-2, "-0", 0, 0},
+ {2, "-101011010", -346, 0},
+ {2, "-101110000100010010110100001110000100110010100001000100110011", 0, "-829870984705741107"},
+ // correct base 9
+ {9, "0", 0, 0},
+ {9, "774", 634, 0},
+ {9, "1231237872637126378", 0, "189269583091367543"},
+ {-9, "-0", 0, 0},
+ {9, "-774", -634, 0},
+ {9, "-1231237872637126378", 0, "-189269583091367543"},
+ // correct base 18
+ {18, "0", 0, 0},
+ {18, "a8", 188, 0},
+ {18, "aabcd3478", 0, "116719998974"},
+ {-18, "-0", 0, 0},
+ {18, "-a8", -188, 0},
+ {18, "-aabcd3478", 0, "-116719998974"},
+ // correct base 27
+ {27, "0", 0, 0},
+ {27, "hdjjjk", 251228513, 0},
+ {27, "2389aidahjdah786ad", 0, "4571349829653128989996813"},
+ {-27, "-0", 0, 0},
+ {27, "-hdjjjk", -251228513, 0},
+ {27, "-2389aidahjdah786ad", 0, "-4571349829653128989996813"},
+ // correct base 36
+ {36, "0", 0, 0},
+ {36, "sa3mx", 47500521, 0},
+ {36, "39xmasdhaxnzxcjkcjgh7893", 0, "2043118606165143938951178056619542151"},
+ {-36, "-0", 0, 0},
+ {36, "-sa3mx", -47500521, 0},
+ {36, "-39xmasdhaxnzxcjkcjgh7893", 0, "-2043118606165143938951178056619542151"},
+ // spaces, uppercase letters
+ {-36, " \t-39xASKMXM44ksxjsDJgh7893", 0, "-2042964800170965112469457130317055623"},
+ // terminating on the letter that does not belong to the set
+ {-36, "sa3mx<3", 47500521, 0},
+ {-10, "-3489x3e3", -3489, 0},
+ {-16, "ABCDEFG", 11259375, 0},
+ // leading zeroes
+ {-36, "-0000sa3mx", -47500521, 0},
+ // incorrect input
+ {-10, "ABCDEF", 0, 0},
+ {-10, "\xff", 0, 0},
+ // handling non-ASCII symbols
+ {-36, "sa3mx\xa3\xb3", 47500521, 0},
+ };
+
+ for (size_t i = 0; i < ARRAY_SIZE(entries); ++i)
+ {
+ const test_entry& e = entries[i];
+
+ int base = abs(e.base);
+
+ // test string ctors
+ number ref = e.rhs ? number(e.rhs) : number(e.value);
+
+ BOOST_CHECK_EQUAL(ref, number(e.lhs, base));
+ BOOST_CHECK_EQUAL(ref, number(std::string(e.lhs), base));
+ BOOST_CHECK_EQUAL(ref, number(std::wstring(e.lhs, e.lhs + strlen(e.lhs)).c_str(), base));
+ BOOST_CHECK_EQUAL(ref, number(std::wstring(e.lhs, e.lhs + strlen(e.lhs)), base));
+
+ if (e.base > 0)
+ {
+ // test conversion
+ BOOST_CHECK_EQUAL(ref.str(base), std::string(e.lhs));
+ BOOST_CHECK(ref.wstr(base) == std::wstring(e.lhs, e.lhs + strlen(e.lhs))); // bug in boost.test - no wstring support
+ }
+ }
+}
+
+int test_main(int argc, char* argv[])
+{
+ test<boost::detail::bigint_gmp_implementation>();
+
+ return 0;
+}

Modified: sandbox/SOC/2007/bigint/libs/bigint/todo.txt
==============================================================================
--- sandbox/SOC/2007/bigint/libs/bigint/todo.txt (original)
+++ sandbox/SOC/2007/bigint/libs/bigint/todo.txt 2007-06-04 15:27:34 EDT (Mon, 04 Jun 2007)
@@ -88,6 +88,9 @@
 + what if there is not enough memory for the request - what happens then? Investigate.
 Status: investigated. abort() is called in this case. The solution for this is complex - read below.
 
++ fix bug in wstr() - sometimes mpz_sizeinbase returns slightly bigger size
+Status: fixed
+
 - remove warnings for both MSVC and GCC
 Status: needs fixing
 
@@ -96,8 +99,11 @@
 + test all ctors for numbers
 Status: implemented
 
-- test all ctors for strings (various bases - 2, 9, 18, 27, 36)
-Status: needs implementing
++ test all ctors for strings (various bases - 2, 9, 18, 27, 36)
+Status: implemented (merged with "test string conversion")
+
++ test string conversion (various bases - 2, 9, 18, 27, 36)
+Status: implemented (merged with "test all ctors for strings")
 
 - test copy construction and assignment
 Status: needs implementing
@@ -117,9 +123,6 @@
 - test bool conversion and operator!
 Status: needs implementing
 
-- test string conversion (various bases - 2, 9, 18, 27, 36)
-Status: needs implementing
-
 - test can_convert_to (char, short, int, int64 - both signed and unsigned, boundary cases)
 Status: needs implementing
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk