Repository: bitcoin/bitcoin
File: src/test/arith_uint256_tests.cpp (C++)

Code before:
    BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32));
    BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32));

    // == and !=
    BOOST_CHECK(R1L != R2L);
    BOOST_CHECK(ZeroL != OneL);
    BOOST_CHECK(OneL != ZeroL);
    BOOST_CHECK(MaxL != ZeroL);
    BOOST_CHECK(~MaxL == ZeroL);
    BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);

    uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
    for (unsigned int i = 0; i < 256; ++i)
    {
        BOOST_CHECK(ZeroL != (OneL << i));
        BOOST_CHECK((OneL << i) != ZeroL);
        BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
        BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
    }
    BOOST_CHECK(ZeroL == (OneL << 256));

    // String Constructor and Copy Constructor
    BOOST_CHECK(arith_uint256S("0x" + R1L.ToString()) == R1L);
    BOOST_CHECK(arith_uint256S("0x" + R2L.ToString()) == R2L);
    BOOST_CHECK(arith_uint256S("0x" + ZeroL.ToString()) == ZeroL);
    BOOST_CHECK(arith_uint256S("0x" + OneL.ToString()) == OneL);
    BOOST_CHECK(arith_uint256S("0x" + MaxL.ToString()) == MaxL);
    BOOST_CHECK(arith_uint256S(R1L.ToString()) == R1L);
    BOOST_CHECK(arith_uint256S("   0x" + R1L.ToString() + "   ") == R1L);
    BOOST_CHECK(arith_uint256S("") == ZeroL);
    BOOST_CHECK(arith_uint256S("1") == OneL);
    BOOST_CHECK(R1L == arith_uint256S(R1ArrayHex));
    BOOST_CHECK(arith_uint256(R1L) == R1L);
    BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L);
    BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);
    BOOST_CHECK(arith_uint256(OneL) == OneL);

    // uint64_t constructor
    BOOST_CHECK((R1L & arith_uint256S("0xffffffffffffffff")) == arith_uint256(R1LLow64));
    BOOST_CHECK(ZeroL == arith_uint256(0));
    BOOST_CHECK(OneL == arith_uint256(1));
    BOOST_CHECK(arith_uint256S("0xffffffffffffffff") == arith_uint256(0xffffffffffffffffULL));

    // Assignment (from base_uint)
    arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
    tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
    tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
    tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
    tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
}


Review comment (suggestion):
since we've already included `setup_common` which contains `std::ostream& operator<<(std::ostream& os, const arith_uint256& num)` we might as well use `BOOST_CHECK_EQUAL` here:
```diff
diff --git a/src/test/arith_uint256_tests.cpp b/src/test/arith_uint256_tests.cpp
index a434a1d401..9a6307c1bd 100644
--- a/src/test/arith_uint256_tests.cpp
+++ b/src/test/arith_uint256_tests.cpp
@@ -64,51 +64,50 @@ static std::string ArrayToString(const unsigned char A[], unsigned int width)
 
 BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
 {
-    BOOST_CHECK(1 == 0+1);
     // constructor arith_uint256(vector<char>):
-    BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32));
-    BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32));
-    BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32));
-    BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32));
-    BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32));
-    BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32));
+    BOOST_CHECK_EQUAL(R1L.ToString(), ArrayToString(R1Array, 32));
+    BOOST_CHECK_EQUAL(R2L.ToString(), ArrayToString(R2Array, 32));
+    BOOST_CHECK_EQUAL(ZeroL.ToString(), ArrayToString(ZeroArray, 32));
+    BOOST_CHECK_EQUAL(OneL.ToString(), ArrayToString(OneArray, 32));
+    BOOST_CHECK_EQUAL(MaxL.ToString(), ArrayToString(MaxArray, 32));
+    BOOST_CHECK_NE(OneL.ToString(), ArrayToString(ZeroArray, 32));
 
     // == and !=
-    BOOST_CHECK(R1L != R2L);
-    BOOST_CHECK(ZeroL != OneL);
-    BOOST_CHECK(OneL != ZeroL);
-    BOOST_CHECK(MaxL != ZeroL);
-    BOOST_CHECK(~MaxL == ZeroL);
-    BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);
+    BOOST_CHECK_NE(R1L, R2L);
+    BOOST_CHECK_NE(ZeroL, OneL);
+    BOOST_CHECK_NE(OneL, ZeroL);
+    BOOST_CHECK_NE(MaxL, ZeroL);
+    BOOST_CHECK_EQUAL(~MaxL, ZeroL);
+    BOOST_CHECK_EQUAL((R1L ^ R2L) ^ R1L, R2L);
 
     uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
     for (unsigned int i = 0; i < 256; ++i)
     {
-        BOOST_CHECK(ZeroL != (OneL << i));
-        BOOST_CHECK((OneL << i) != ZeroL);
-        BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
-        BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
+        BOOST_CHECK_NE(ZeroL, OneL << i);
+        BOOST_CHECK_NE(OneL << i, ZeroL);
+        BOOST_CHECK_NE(R1L, R1L ^ (OneL << i));
+        BOOST_CHECK_NE(arith_uint256(Tmp64) ^ (OneL << i), Tmp64);
     }
-    BOOST_CHECK(ZeroL == (OneL << 256));
+    BOOST_CHECK_EQUAL(ZeroL, OneL << 256);
 
     // Copy Constructor
-    BOOST_CHECK(arith_uint256(R1L) == R1L);
-    BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L);
-    BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);
-    BOOST_CHECK(arith_uint256(OneL) == OneL);
+    BOOST_CHECK_EQUAL(arith_uint256(R1L), R1L);
+    BOOST_CHECK_EQUAL(arith_uint256(R1L ^ R2L) ^ R2L, R1L);
+    BOOST_CHECK_EQUAL(arith_uint256(ZeroL), ZeroL);
+    BOOST_CHECK_EQUAL(arith_uint256(OneL), OneL);
 
     // uint64_t constructor
-    BOOST_CHECK_EQUAL((R1L & arith_uint256{0xffffffffffffffff}), arith_uint256{R1LLow64});
+    BOOST_CHECK_EQUAL(R1L & arith_uint256{0xffffffffffffffff}, arith_uint256{R1LLow64});
     BOOST_CHECK_EQUAL(ZeroL, arith_uint256{0});
     BOOST_CHECK_EQUAL(OneL, arith_uint256{1});
     BOOST_CHECK_EQUAL(arith_uint256{0xffffffffffffffff}, arith_uint256{0xffffffffffffffffULL});
 
     // Assignment (from base_uint)
-    arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
-    tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
-    tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
-    tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
-    tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
+    arith_uint256 tmpL = ~ZeroL; BOOST_CHECK_EQUAL(tmpL, ~ZeroL);
+    tmpL = ~OneL; BOOST_CHECK_EQUAL(tmpL, ~OneL);
+    tmpL = ~R1L; BOOST_CHECK_EQUAL(tmpL, ~R1L);
+    tmpL = ~R2L; BOOST_CHECK_EQUAL(tmpL, ~R2L);
+    tmpL = ~MaxL; BOOST_CHECK_EQUAL(tmpL, ~MaxL);
 }
 
 static void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
```

I understand of course if you think some of these are unrelated, though some of the comments are weird, e.g. `// constructor arith_uint256(vector<char>)` doesn't seem to test any constructors directly.

Code after:
    BOOST_CHECK(ZeroL != OneL);
    BOOST_CHECK(OneL != ZeroL);
    BOOST_CHECK(MaxL != ZeroL);
    BOOST_CHECK(~MaxL == ZeroL);
    BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);

    uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
    for (unsigned int i = 0; i < 256; ++i)
    {
        BOOST_CHECK(ZeroL != (OneL << i));
        BOOST_CHECK((OneL << i) != ZeroL);
        BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
        BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
    }
    BOOST_CHECK(ZeroL == (OneL << 256));

    // Construct from hex string
    BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R1L.ToString()).value()), R1L);
    BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R2L.ToString()).value()), R2L);
    BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(ZeroL.ToString()).value()), ZeroL);
    BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(OneL.ToString()).value()), OneL);
    BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(MaxL.ToString()).value()), MaxL);
    BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R1ArrayHex).value()), R1L);

    // Copy constructor
    BOOST_CHECK(arith_uint256(R1L) == R1L);
    BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L);
    BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);
    BOOST_CHECK(arith_uint256(OneL) == OneL);

    // uint64_t constructor
    BOOST_CHECK_EQUAL(R1L & arith_uint256{0xffffffffffffffff}, arith_uint256{R1LLow64});
    BOOST_CHECK_EQUAL(ZeroL, arith_uint256{0});
    BOOST_CHECK_EQUAL(OneL, arith_uint256{1});
    BOOST_CHECK_EQUAL(arith_uint256{0xffffffffffffffff}, arith_uint256{0xffffffffffffffffULL});

    // Assignment (from base_uint)
    arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
    tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
    tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
    tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
    tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
}

static void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
{
    for (unsigned int T=0; T < arrayLength; ++T)
    {
        unsigned int F = (T+bitsToShift/8);
        if (F < arrayLength)
            to[T] = uint8_t(from[F] >> (bitsToShift % 8));