Skip to content

Instantly share code, notes, and snippets.

@NotFounds
Created January 24, 2017 13:17
Show Gist options
  • Save NotFounds/21485e6cf245fcf4ef05781daf1c49f9 to your computer and use it in GitHub Desktop.
Save NotFounds/21485e6cf245fcf4ef05781daf1c49f9 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cassert>
#include "Matrix.h"
using namespace std;
// Definition: Matrix foo
// Definition: Matrix foo(row, col)
// Definition: Matrix foo(row, col, double[])
void test_matrix()
{
Matrix a(1, 2, new double[2] {0, 1});
assert(a(0, 0) == 0);
assert(a(0, 1) == 1);
Matrix b(2, 1, new double[2]{ 2, -1 });
assert(b(0, 0) == 2);
assert(b(1, 0) == -1);
cout << "[PASSED] test_matrix" << endl;
}
// Editable: matrix[row][col]
// ReadOnly: matrix(row, col)
void test_refer()
{
Matrix a(2, 2);
a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 3;
a[1][1] = 4;
assert(a(0, 0) == 1);
assert(a(0, 1) == 2);
assert(a(1, 0) == 3);
assert(a(1, 1) == 4);
cout << "[PASSED] test_refer" << endl;
}
// Matrix = Matrix
void test_assignment()
{
Matrix a(1, 2, new double[2]{ 0, 1 });
Matrix b = a;
assert(b(0, 0) == 0);
assert(b(0, 1) == 1);
Matrix c;
c = a;
assert(c(0, 0) == 0);
assert(c(0, 1) == 1);
cout << "[PASSED] test_assignment" << endl;
}
// Matrix + Matrix
// Matrix -= Matrix
void test_add()
{
Matrix a(2, 1, new double[2] {0, 5});
Matrix b(2, 1, new double[2] {1, -2});
Matrix c = a + b;
assert(c(0, 0) == 1);
assert(c(1, 0) == 3);
Matrix d = a;
d += b;
assert(d(0, 0) == 1);
assert(d(1, 0) == 3);
cout << "[PASSED] test_add" << endl;
}
// Matrix - Matrix
// Matrix -= Matrix
void test_sub()
{
Matrix a(2, 1, new double[2] {0, 5});
Matrix b(2, 1, new double[2] {1, -2});
Matrix c = a - b;
assert(c(0, 0) == -1);
assert(c(1, 0) == 7);
Matrix d = a;
d -= b;
assert(d(0, 0) == -1);
assert(d(1, 0) == 7);
cout << "[PASSED] test_sub" << endl;
}
// implemented: Matrix * double
// unimplemented: double * Matrix
void test_multi()
{
Matrix a(2, 1, new double[2] {0, 5});
Matrix b(1, 2, new double[2] {1, -2});
Matrix c = a * b;
assert(c(0, 0) == 0);
assert(c(0, 1) == 0);
assert(c(1, 0) == 5);
assert(c(1, 1) == -10);
Matrix d = a * 2;
assert(d(0, 0) == 0);
assert(d(1, 0) == 10);
cout << "[PASSED] test_multi" << endl;
}
// implemented: Matrix / double
// unimplemented: double / Matrix
void test_div()
{
Matrix a(2, 1, new double[2]{ 0, 5 });
Matrix c = a / 2;
assert(c(0, 0) == 0);
assert(c(1, 0) == 2.5);
cout << "[PASSED] test_div" << endl;
}
// Matrix.transpose()
void test_transpose()
{
Matrix a(2, 1, new double[2] {0, 5});
Matrix b = a.transpose();
assert(b(0, 0) == 0);
assert(b(0, 1) == 5);
cout << "[PASSED] test_transpose" << endl;
}
// Matrix == Matrix
// Matrix != Matrix
void test_equal()
{
Matrix a(2, 1, new double[2]{ 0, 5 });
Matrix b(2, 1, new double[2]{ 0, 5 });
Matrix c(1, 2, new double[2]{ 0, 5 });
Matrix d(1, 2, new double[2]{ 1, 5 });
assert(a == b);
assert(a != c);
assert(a != d);
cout << "[PASSED] test_equal" << endl;
}
// Matrix.random(min, max)
// Matrix.random(min, max, seed)
void sample_random()
{
Matrix a(2, 1);
a.random(0, 1);/*
cout << a(0, 0) << endl;
cout << a(1, 0) << endl;*/
}
int main()
{
test_matrix();
test_refer();
test_assignment();
test_add();
test_sub();
test_multi();
test_div();
test_transpose();
test_equal();
sample_random();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment