-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit tests for V2, which tests functionalities of V2
- Loading branch information
Showing
9 changed files
with
721 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* test_bitmatrix.cpp | ||
* | ||
* Created on: Mar 11, 2015 | ||
* Author: mtamayo | ||
*/ | ||
|
||
#include "../../../contrib/gtest/gtest.h" | ||
#include "../../main/cpp/BitVector.h" | ||
#include "../../main/cpp/BitMatrix.h" | ||
#include <string> | ||
using namespace testing; | ||
|
||
#define N 64 | ||
|
||
TEST(BitMatrixTest, testProvidedConstructors) { | ||
BitMatrix<N> m = BitMatrix<N>::randomMatrix(); | ||
BitMatrix<N> m2 = m; | ||
|
||
if( m[ 10 ][ 25 ] ) { | ||
m[ 10 ].clear( 25 ); | ||
} else { | ||
m[ 10 ].set( 25 ); | ||
} | ||
|
||
ASSERT_TRUE( m.operator!=( m2 ) ); | ||
} | ||
|
||
TEST(BitMatrixTest, testIdentity){ | ||
BitMatrix<N> I = BitMatrix<N>::identityMatrix(); | ||
ASSERT_TRUE(I.isIdentity()); | ||
BitMatrix<N> O = BitMatrix<N>::zeroMatrix(); //square zero matrix | ||
ASSERT_FALSE(O.isIdentity()); | ||
} | ||
|
||
TEST(BitMatrixTests, testRandomMatrix) { | ||
BitMatrix<N> m = BitMatrix<N>::randomMatrix(); //square random matrix | ||
|
||
printf("Generated random matrix.\n"); | ||
{ | ||
const char * unformatted_msg = "Row count is incorrect: %d"; | ||
size_t bufLen = strlen(unformatted_msg) + 32; | ||
char trace_msg[bufLen]; | ||
snprintf(trace_msg, bufLen, unformatted_msg, m.rowCount()); | ||
SCOPED_TRACE(trace_msg); | ||
|
||
ASSERT_TRUE(m.rowCount() == 64); | ||
} | ||
|
||
for (int i = 0; i < m.rowCount(); ++i) { | ||
SCOPED_TRACE("Unexpected zero vector."); | ||
ASSERT_FALSE(m[i].isZero()); | ||
} | ||
} | ||
|
||
TEST(BitMatrixTests, testEqual){ | ||
BitMatrix<N> I = BitMatrix<N>::identityMatrix(); | ||
ASSERT_TRUE(I.equals(I)); | ||
BitMatrix<N> J = BitMatrix<N>::identityMatrix(); | ||
ASSERT_TRUE(I.equals(J)); | ||
ASSERT_TRUE(J.equals(I)); | ||
BitMatrix<N,N> R = BitMatrix<N,N>::randomInvertibleMatrix(); | ||
ASSERT_TRUE(R.equals(R)); | ||
BitMatrix<2*N,N> S = BitMatrix<2*N,N>::randomMatrix(); | ||
ASSERT_TRUE(S.equals(S)); | ||
} | ||
|
||
TEST(BitMatrixTests, testAssignment){ | ||
BitMatrix<N> A = BitMatrix<N>::randomInvertibleMatrix(); | ||
A.set(0,0); | ||
A.set(1,1); | ||
A.set(N-1,N-1); | ||
ASSERT_TRUE(A.get(0,0) && A.get(1,1) && A.get(N-1,N-1)); | ||
} | ||
|
||
TEST(BitMatrixTests, testSolve) { | ||
ASSERT_TRUE( 1+5 == 6 ); | ||
BitVector<N> v = BitVector<N>::randomVector(); | ||
BitMatrix<N> M = BitMatrix<N>::randomInvertibleMatrix(); | ||
BitVector<N> x = M.solve(v); | ||
BitVector<N> mx = M * x; | ||
ASSERT_TRUE(mx.equals(v)); | ||
} | ||
|
||
TEST(BitMatrixTests, testInverse) { | ||
ASSERT_TRUE(1+5 == 6); | ||
BitMatrix<N> M = BitMatrix<N>::randomInvertibleMatrix(); | ||
BitMatrix<N> Mi = M.inv(); | ||
BitMatrix<N> Il = M*Mi; | ||
BitMatrix<N> Ir = Mi*M; | ||
ASSERT_TRUE(Il.isIdentity()); | ||
ASSERT_TRUE(Ir.isIdentity()); | ||
} | ||
|
||
TEST(BitMatrixTests, testRightInverse){ | ||
BitMatrix<N> M1 = BitMatrix<N>::randomInvertibleMatrix(); | ||
BitMatrix<N> M2 = BitMatrix<N>::randomInvertibleMatrix(); | ||
BitMatrix<N, 2*N> M = BitMatrix<N, 2*N>::augH(M1, M2); | ||
BitMatrix<2*N, N> Mi = M.rightInverse(); | ||
BitMatrix<N> J = M * Mi; | ||
ASSERT_TRUE(J.isIdentity()); | ||
} | ||
|
||
TEST(BitMatrixTests, testLeftInverse){ | ||
BitMatrix<N> M1 = BitMatrix<N>::randomInvertibleMatrix(); | ||
BitMatrix<N> M2 = BitMatrix<N>::randomInvertibleMatrix(); | ||
BitMatrix<2*N, N> M = BitMatrix<2*N, N>::augV(M1, M2); | ||
BitMatrix<N, 2*N> Mi = M.leftInverse(); | ||
BitMatrix<N> J = Mi * M; | ||
ASSERT_TRUE(J.isIdentity()); | ||
} | ||
|
||
TEST(BitMatrixTests, testSplit) { | ||
BitMatrix<2*N> I = BitMatrix<2*N>::identityMatrix(); | ||
BitMatrix<2*N,N> I1 = I.splitH2(0); | ||
BitMatrix<2*N,N> I2 = I.splitH2(1); | ||
BitMatrix<N> I11 = I1.splitV2(0); | ||
BitMatrix<N> I21 = I1.splitV2(1); | ||
BitMatrix<N> I12 = I2.splitV2(0); | ||
BitMatrix<N> I22 = I2.splitV2(1); | ||
ASSERT_TRUE(I11.isIdentity()); | ||
ASSERT_FALSE(I12.isIdentity()); | ||
ASSERT_FALSE(I21.isIdentity()); | ||
ASSERT_TRUE(I22.isIdentity()); | ||
|
||
//TODO: Need a more through test here. | ||
BitMatrix<3*N> J = BitMatrix<3*N>::identityMatrix(); | ||
BitMatrix<3*N,N> J1 = J.splitH3(0); | ||
BitMatrix<N,N> J11 = J1.splitV3(0); | ||
ASSERT_TRUE(J11.isIdentity()); | ||
} | ||
|
||
TEST(BitMatrixTest, testAug){ | ||
BitMatrix<N> I = BitMatrix<N>::identityMatrix(); | ||
BitMatrix<N> O = BitMatrix<N>::zeroMatrix(); | ||
BitMatrix<N,2*N> IO = BitMatrix<N,2*N>::augH<N,N>(I, O); | ||
BitMatrix<N,2*N> OI = BitMatrix<N,2*N>::augH<N,N>(O, I); | ||
BitMatrix<2*N,2*N> IOOI = BitMatrix<2*N,2*N>::augV(IO, OI); | ||
ASSERT_TRUE(IOOI.isIdentity()); | ||
} | ||
|
||
TEST(BitMatrixTests, testTranspose){ | ||
BitMatrix<N> I = BitMatrix<N>::identityMatrix(); | ||
ASSERT_TRUE(I.isIdentity()); | ||
BitMatrix<N,N> It = I.transpose(); | ||
ASSERT_TRUE(It.isIdentity()); | ||
|
||
BitMatrix<N> S = BitMatrix<N>::randomMatrix(); //square random matrix | ||
BitMatrix<N,N> St = S.transpose(); | ||
BitMatrix<N,N> Stt = St.transpose(); | ||
ASSERT_TRUE(S.equals(Stt)); | ||
ASSERT_TRUE(Stt.equals(S)); | ||
|
||
BitMatrix<N,2*N> R = BitMatrix<N,2*N>::randomMatrix(); | ||
BitMatrix<2*N,N> Rt = R.transpose(); | ||
BitMatrix<N,2*N> Rtt = Rt.transpose(); | ||
ASSERT_TRUE(R.equals(Rtt)); | ||
ASSERT_TRUE(Rtt.equals(R)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#include "../../../contrib/gtest/gtest.h" | ||
#include "../../main/cpp/BitVector.h" | ||
|
||
using namespace testing; | ||
|
||
TEST(BitVectorTests, test_smallrandom){ | ||
BitVector<64> bv = BitVector<64>::randomSmallVector(); | ||
ASSERT_TRUE(!bv.get(0)); | ||
} | ||
|
||
TEST(BitVectorTests, test_clear) { | ||
BitVector<64> bv = BitVector<64>::randomVector(); | ||
|
||
while( bv.isZero() ) { | ||
bv = BitVector<64>::randomVector(); | ||
} | ||
|
||
ASSERT_FALSE( bv.isZero() ); | ||
|
||
bv.zero(); | ||
|
||
ASSERT_TRUE( bv.isZero() ); | ||
} | ||
|
||
TEST(BitVectorTests, test_equals) { | ||
BitVector<64> bv = BitVector<64>::randomVector(); | ||
BitVector<64> bv2 = BitVector<64>::randomVector(); | ||
BitVector<64> bv1 = bv; | ||
|
||
//Make sure that equals isn't totally busted | ||
ASSERT_TRUE(bv == bv); | ||
|
||
while( bv2 == bv ) { | ||
bv2 = BitVector<64>::randomVector(); | ||
} | ||
|
||
//Make sure the copy assignment makes a deep copy. | ||
ASSERT_TRUE( bv.elements() != bv1.elements() ); | ||
ASSERT_TRUE(bv != bv2 ); | ||
ASSERT_TRUE(bv1 != bv2 ); | ||
ASSERT_TRUE(bv == bv1 ); | ||
} | ||
|
||
TEST(BitVectorTests, test_zero) { | ||
BitVector<64> v = BitVector<64>::zeroVector(); | ||
ASSERT_TRUE(v.isZero()); | ||
} | ||
|
||
TEST(BitVectorTests, test_and) { | ||
BitVector<64> v1 = BitVector<64>::zeroVector(); | ||
BitVector<64> v2 = BitVector<64>::randomVector(); | ||
BitVector<64> v3 = BitVector<64>::randomVector(); | ||
ASSERT_TRUE( (v1&v2).isZero() ); | ||
|
||
ASSERT_EQ( (v2&v3).elements()[0] , ((v2.elements())[0]&(v3.elements()[0])) ); | ||
} | ||
|
||
TEST(BitVectorTests, test_xor) { | ||
BitVector<64> v1 = BitVector<64>::zeroVector(); | ||
BitVector<64> v2 = BitVector<64>::randomVector(); | ||
BitVector<64> v3 = BitVector<64>::randomVector(); | ||
ASSERT_EQ(v2, v1^v2); | ||
|
||
ASSERT_TRUE( (v2^v3).elements()[0] == (v2.elements()[0]^v3.elements()[0]) ); | ||
} | ||
|
||
TEST(BitVectorTests, test_or) { | ||
BitVector<64> v1 = BitVector<64>::zeroVector(); | ||
BitVector<64> v2 = BitVector<64>::randomVector(); | ||
BitVector<64> v3 = BitVector<64>::randomVector(); | ||
ASSERT_EQ(v2, v1|v2); | ||
|
||
ASSERT_TRUE( (v2|v3).elements()[0] == (v2.elements()[0]|v3.elements()[0]) ); | ||
} | ||
|
||
TEST(BitVectorTests, test_parity) { | ||
BitVector<64> v = BitVector<64>::zeroVector(); | ||
ASSERT_FALSE(v.parity()); | ||
|
||
v.elements()[ 0 ] = 0x1; | ||
ASSERT_TRUE( v.parity() ); | ||
|
||
v.elements()[ 0 ] = 0xFF; | ||
ASSERT_FALSE( v.parity() ); | ||
} | ||
|
||
TEST(BitVectorTests, test_shift) { | ||
BitVector<64> v = BitVector<64>::randomSmallVector(); | ||
BitVector<64> w = v.leftShift(1); | ||
ASSERT_FALSE(w.get(63)); | ||
bool shift = true; | ||
for(int i = 0; i < 63; ++i){ | ||
if(v.get(i+1) != w.get(i)){ | ||
shift = false; | ||
break; | ||
} | ||
} | ||
ASSERT_TRUE(shift); | ||
} | ||
|
||
TEST(BitVectorTests, test_add) { | ||
BitVector<64> x = BitVector<64>::randomSmallVector(); | ||
BitVector<64> y = BitVector<64>::randomSmallVector(); | ||
std::cout << "x: " << endl; | ||
x.print(); | ||
std::cout << "y: " << endl; | ||
y.print(); | ||
std::cout << "x + y: " << endl; | ||
BitVector<64> sum = x + y; | ||
sum.print(); | ||
ASSERT_TRUE(true); | ||
} | ||
|
||
TEST(BitVectorTests, test_mult) { | ||
BitVector<64> x = BitVector<64>::zeroVector(); | ||
BitVector<64> y = BitVector<64>::zeroVector(); | ||
x.set(63); x.set(60); | ||
y.set(62); y.set(61); | ||
std::cout << "x: " << endl; | ||
x.print(); | ||
std::cout << "y: " << endl; | ||
y.print(); | ||
std::cout << "x * y: " << endl; | ||
BitVector<64> sum = x * y; | ||
sum.print(); | ||
ASSERT_TRUE(true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "../../../contrib/gtest/gtest.h" | ||
|
||
using namespace testing; | ||
|
||
int main(int argc, char **argv) { | ||
testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
Oops, something went wrong.