Browse Source

[3015] Type of IntElement is changed from long int to int64_t.
Element::create accepts char, short, int, long, long long, int32_t and int64_t.
IntElement->intValue() returns int64_t.
IntElement->getValue() and IntElement->setValue() accept int64_t only.

Kazunori Fujiwara 11 years ago
parent
commit
5d8b4032ea
3 changed files with 58 additions and 24 deletions
  1. 13 7
      src/lib/cc/data.cc
  2. 12 10
      src/lib/cc/data.h
  3. 33 7
      src/lib/cc/tests/data_unittests.cc

+ 13 - 7
src/lib/cc/data.cc

@@ -60,7 +60,7 @@ Element::toWire(std::ostream& ss) const {
 }
 
 bool
-Element::getValue(long int&) const {
+Element::getValue(int64_t&) const {
     return (false);
 }
 
@@ -90,7 +90,7 @@ Element::getValue(std::map<std::string, ConstElementPtr>&) const {
 }
 
 bool
-Element::setValue(const long int) {
+Element::setValue(const int64_t) {
     return (false);
 }
 
@@ -208,8 +208,8 @@ Element::create() {
 }
 
 ElementPtr
-Element::create(const long int i) {
-    return (ElementPtr(new IntElement(i)));
+Element::create(const long long int i) {
+    return (ElementPtr(new IntElement(static_cast<int64_t>(i))));
 }
 
 ElementPtr
@@ -391,9 +391,15 @@ numberFromStringstream(std::istream& in, int& pos) {
 // Should we change from IntElement and DoubleElement to NumberElement
 // that can also hold an e value? (and have specific getters if the
 // value is larger than an int can handle)
+//
+// Type of IntElement is changed from long int to int64_t.
+// However, strtoint64_t function does not exist.
+// It is assumed that "long long" and int64_t are the same sizes.
+// strtoll is used to convert string to integer.
+//
 ElementPtr
 fromStringstreamNumber(std::istream& in, int& pos) {
-    long int i;
+    long long int i;
     double d = 0.0;
     bool is_double = false;
     char* endptr;
@@ -401,7 +407,7 @@ fromStringstreamNumber(std::istream& in, int& pos) {
     std::string number = numberFromStringstream(in, pos);
 
     errno = 0;
-    i = strtol(number.c_str(), &endptr, 10);
+    i = strtoll(number.c_str(), &endptr, 10);
     if (*endptr != '\0') {
         const char* ptr;
         errno = 0;
@@ -415,7 +421,7 @@ fromStringstreamNumber(std::istream& in, int& pos) {
             }
         }
     } else {
-        if ((i == LONG_MAX || i == LONG_MIN) && errno != 0) {
+        if ((i == LLONG_MAX || i == LLONG_MIN) && errno != 0) {
             isc_throw(JSONError, std::string("Number overflow: ") + number);
         }
     }

+ 12 - 10
src/lib/cc/data.h

@@ -124,7 +124,7 @@ public:
     /// If you want an exception-safe getter method, use
     /// getValue() below
     //@{
-    virtual long int intValue() const
+    virtual int64_t intValue() const
     { isc_throw(TypeError, "intValue() called on non-integer Element"); };
     virtual double doubleValue() const
     { isc_throw(TypeError, "doubleValue() called on non-double Element"); };
@@ -151,7 +151,7 @@ public:
     /// data to the given reference and returning true
     ///
     //@{
-    virtual bool getValue(long int& t) const;
+    virtual bool getValue(int64_t& t) const;
     virtual bool getValue(double& t) const;
     virtual bool getValue(bool& t) const;
     virtual bool getValue(std::string& t) const;
@@ -167,7 +167,7 @@ public:
     /// is of the correct type
     ///
     //@{
-    virtual bool setValue(const long int v);
+    virtual bool setValue(const int64_t v);
     virtual bool setValue(const double v);
     virtual bool setValue(const bool t);
     virtual bool setValue(const std::string& v);
@@ -273,8 +273,9 @@ public:
     /// represents an empty value, and is created with Element::create())
     //@{
     static ElementPtr create();
-    static ElementPtr create(const long int i);
-    static ElementPtr create(const int i) { return (create(static_cast<long int>(i))); };
+    static ElementPtr create(const long long int i);
+    static ElementPtr create(const int i) { return (create(static_cast<long long int>(i))); };
+    static ElementPtr create(const long int i) { return (create(static_cast<long long int>(i))); };
     static ElementPtr create(const double d);
     static ElementPtr create(const bool b);
     static ElementPtr create(const std::string& s);
@@ -371,15 +372,16 @@ public:
 };
 
 class IntElement : public Element {
-    long int i;
+    int64_t i;
+private:
 
 public:
-    IntElement(long int v) : Element(integer), i(v) { }
-    long int intValue() const { return (i); }
+    IntElement(int64_t v) : Element(integer), i(v) { }
+    int64_t intValue() const { return (i); }
     using Element::getValue;
-    bool getValue(long int& t) const { t = i; return (true); }
+    bool getValue(int64_t& t) const { t = i; return (true); }
     using Element::setValue;
-    bool setValue(const long int v) { i = v; return (true); }
+    bool setValue(int64_t v) { i = v; return (true); }
     void toJSON(std::ostream& ss) const;
     bool equals(const Element& other) const;
 };

+ 33 - 7
src/lib/cc/tests/data_unittests.cc

@@ -194,7 +194,10 @@ template <typename T>
 void
 testGetValueInt() {
     T el;
-    long int i;
+    int64_t i;
+    int32_t i32;
+    long l;
+    long long ll;
     double d;
     bool b;
     std::string s;
@@ -215,13 +218,36 @@ testGetValueInt() {
     EXPECT_FALSE(el->getValue(v));
     EXPECT_FALSE(el->getValue(m));
     EXPECT_EQ(1, i);
+
+    el = Element::create(9223372036854775807L);
+    EXPECT_NO_THROW(el->intValue());
+    EXPECT_TRUE(el->getValue(i));
+    EXPECT_EQ(9223372036854775807, i);
+
+    ll = 9223372036854775807L;
+    el = Element::create(ll);
+    EXPECT_NO_THROW(el->intValue());
+    EXPECT_TRUE(el->getValue(i));
+    EXPECT_EQ(ll, i);
+
+    i32 = 2147483647;
+    el = Element::create(i32);
+    EXPECT_NO_THROW(el->intValue());
+    EXPECT_TRUE(el->getValue(i));
+    EXPECT_EQ(i32, i);
+
+    l = 2147483647;
+    el = Element::create(l);
+    EXPECT_NO_THROW(el->intValue());
+    EXPECT_TRUE(el->getValue(i));
+    EXPECT_EQ(l, i);
 }
 
 template <typename T>
 void
 testGetValueDouble() {
     T el;
-    long int i;
+    int64_t i;
     double d;
     bool b;
     std::string s;
@@ -248,7 +274,7 @@ template <typename T>
 void
 testGetValueBool() {
     T el;
-    long int i;
+    int64_t i;
     double d;
     bool b;
     std::string s;
@@ -275,7 +301,7 @@ template <typename T>
 void
 testGetValueString() {
     T el;
-    long int i;
+    int64_t i;
     double d;
     bool b;
     std::string s;
@@ -302,7 +328,7 @@ template <typename T>
 void
 testGetValueList() {
     T el;
-    long int i;
+    int64_t i;
     double d;
     bool b;
     std::string s;
@@ -329,7 +355,7 @@ template <typename T>
 void
 testGetValueMap() {
     T el;
-    long int i;
+    int64_t i;
     double d;
     bool b;
     std::string s;
@@ -357,7 +383,7 @@ TEST(Element, create_and_value_throws) {
     // incorrect type is requested
     ElementPtr el;
     ConstElementPtr cel;
-    long int i = 0;
+    int64_t i = 0;
     double d = 0.0;
     bool b = false;
     std::string s("asdf");