Skip to content

Commit 26cd7a0

Browse files
authored
Merge pull request #41 from saxbophone/josh/implement-string-ctor
Implement string constructor
2 parents c30ac95 + c9f9577 commit 26cd7a0

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

arby/src/arby.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515

1616
namespace com::saxbophone::arby {
17-
Uint::Uint(std::string digits) {}
17+
Uint::Uint(std::string digits)
18+
// use user-defined-literal to convert the digits in the string
19+
: _digits(operator "" _uarb(digits.c_str())._digits)
20+
{}
1821

1922
std::string Uint::_stringify_for_base() const {
2023
// XXX: only base-10 implemented for now

tests/basic_arithmetic.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <compare>
22
#include <limits>
33
#include <stdexcept>
4+
#include <string>
45

56
#include <catch2/catch.hpp>
67

@@ -65,6 +66,15 @@ TEST_CASE("arby::Uint(UINT_MAX) and (uintmax_t)arby::Uint", "[ctor]") {
6566
CHECK((uintmax_t)output == std::numeric_limits<uintmax_t>::max());
6667
}
6768

69+
TEST_CASE("arby::Uint(std::string)", "[ctor]") {
70+
uintmax_t value = GENERATE(take(1000, random((uintmax_t)0, std::numeric_limits<uintmax_t>::max())));
71+
std::string string = std::to_string(value);
72+
73+
arby::Uint object = string;
74+
75+
CHECK((uintmax_t)object == value);
76+
}
77+
6878
// only inlude these tests if we have library support for constexpr vector
6979
#ifdef __cpp_lib_constexpr_vector
7080
TEST_CASE("constexpr arby::Uint", "[constexpr]") {

0 commit comments

Comments
 (0)