Permalink
Browse files

Reimplemented to_int64()

  • Loading branch information...
1 parent 68703c3 commit dddd08da7a9fb5200f0100acc1e2731ac796d6f1 @saleyn committed Jul 11, 2014
Showing with 27 additions and 3 deletions.
  1. +16 −0 include/utxx/string.hpp
  2. +3 −3 test/test_concurrent_spsc_queue.cpp
  3. +8 −0 test/test_string.cpp
View
@@ -48,16 +48,32 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
namespace utxx {
+ /// Return the static length of an array of type T
template <typename T, int N>
size_t length(const T (&a)[N]) {
return N;
}
+ /// Return the static length of a character string
template <int N>
size_t length(const char (&a)[N]) {
return N-1;
}
+ /// Convert a string to an integer value
+ /// \code
+ /// std::cout << to_int64("\1\2") << std::endl;
+ /// output: 258 // I.e. (1 << 8 | 2)
+ /// \endcode
+ constexpr uint64_t to_int64(const char* a_str, size_t sz) {
+ return sz ? (to_int64(a_str, sz-1) << 8 | (uint8_t)a_str[sz-1]) : 0;
+ }
+
+ template <int N>
+ constexpr uint64_t to_int64(const char (&a)[N]) {
+ return to_int64(a, N-1);
+ }
+
/// Find the position of character \a c.
/// @return pointer to the position of character \a c, or \a end if
/// no character \a c is found.
@@ -44,7 +44,7 @@ struct PerfTest {
}
void producer() {
- for (int i = 0; i < traits_.limit(); ++i)
+ for (int i = 0; i < (int)traits_.limit(); ++i)
while (!queue_.push(traits_.generate()));
}
@@ -90,7 +90,7 @@ struct CorrectnessTest {
{
const size_t testSize = traits_.limit();
testData_.reserve(testSize);
- for (int i = 0; i < testSize; ++i)
+ for (size_t i = 0; i < testSize; ++i)
testData_.push_back(traits_.generate());
}
@@ -197,7 +197,7 @@ BOOST_AUTO_TEST_CASE( test_concurrent_spsc_empty ) {
BOOST_REQUIRE(queue.full()); // Tricky: full after 3 writes, not 2.
BOOST_REQUIRE(!queue.push(4));
- BOOST_REQUIRE_EQUAL(queue.count(), 3);
+ BOOST_REQUIRE_EQUAL(queue.count(), 3u);
}
BOOST_AUTO_TEST_CASE( test_concurrent_spsc_correctness ) {
View
@@ -71,6 +71,14 @@ BOOST_AUTO_TEST_CASE( test_string_find_index )
BOOST_REQUIRE(OP_UNDEFINED == utxx::find_index<op_type>(s_ops, "xxx"));
}
+BOOST_AUTO_TEST_CASE( test_string_to_int64 )
+{
+ BOOST_REQUIRE_EQUAL(1u, to_int64("\1"));
+ BOOST_REQUIRE_EQUAL(258u, to_int64("\1\2"));
+ BOOST_REQUIRE_EQUAL(66051u, to_int64("\1\2\3"));
+ BOOST_REQUIRE_EQUAL(4276803u, to_int64("ABC"));
+}
+
BOOST_AUTO_TEST_CASE( test_string_nocase )
{
string_nocase s("AbcDe123");

0 comments on commit dddd08d

Please sign in to comment.