Skip to content

Instantly share code, notes, and snippets.

@mkondratek
Last active June 5, 2018 09:45
Show Gist options
  • Save mkondratek/e269dd2f17a8223532e4f6b466506007 to your computer and use it in GitHub Desktop.
Save mkondratek/e269dd2f17a8223532e4f6b466506007 to your computer and use it in GitHub Desktop.
zadanie_F_test
#include <iostream>
#include <cstdarg>
#include <string>
#include <sstream>
#include <stdio.h>
using namespace std;
#include "POLYNOMIAL.cpp"
int POLYNOMIAL::overloaded = 0;
void mLAssert(int line, std::string const& actual, std::string const& excpected) {
if (actual != excpected) {
std::cerr << "in line: " << line
<< " Assert failed\n\texcpected " << excpected
<< "\n\t got " << actual << "\n";
}
}
void mLAssertTrue(int line, bool const& val) {
if (!val) {
std::cerr << "in line: " << line
<< " AssertTrue failed\n";
}
}
#define mAssert(actual, excpected) mLAssert(__LINE__, (actual), (excpected))
#define mAssertTrue(val) mLAssertTrue(__LINE__, (val))
void divTest(int line, POLYNOMIAL const& p0, POLYNOMIAL const& p1) {
POLYNOMIAL mul(POLYNOMIAL(p0) * p1);
mLAssert(line, (mul / p0).str(), p1.str());
mLAssert(line, (mul % p0).str(), "( 0 )");
mLAssert(line, (mul / p1).str(), p0.str());
mLAssert(line, (mul % p1).str(), "( 0 )");
}
int main() {
POLYNOMIAL p0(2, 4, -6, 8);
mAssert(p0.str(), "( 2, -3, 4 )");
mAssert(itostr(POLYNOMIAL::overloaded), itostr(0));
mAssert(POLYNOMIAL(-5).str(), "( -1 )");
POLYNOMIAL p1(4, 1, 0, -5, 0, 3);
mAssert(p1.str(), "( 1, 0, -5, 0, 3 )");
std::stringstream ss;
ss << p1;
mAssert(ss.str(), "( 1, 0, -5, 0, 3 )");
std::istringstream iss("4 1 2 3 4 5");
POLYNOMIAL p2;
iss >> p2;
mAssert(p2.str(), "( 1, 2, 3, 4, 5 )");
POLYNOMIAL p3 = p2;
mAssert(p3.str(), "( 1, 2, 3, 4, 5 )");
p2 <<= 2;
p3 >>= 2;
mAssert(p2.str(), "( 3, 4, 5 )");
mAssert(p3.str(), "( 0, 0, 1, 2, 3, 4, 5 )");
mAssert((p2 + p3).str(), "( 3, 4, 6, 2, 3, 4, 5 )");
mAssert((p3 + p2).str(), "( 3, 4, 6, 2, 3, 4, 5 )");
mAssert((p2 - p3).str(), "( 3, 4, 4, -2, -3, -4, -5 )");
mAssert((-p2).str(), "( -3, -4, -5 )");
mAssert((-(-p2)).str(), "( 3, 4, 5 )");
mAssert((-(-(-p2))).str(), "( -3, -4, -5 )");
POLYNOMIAL p4(1, 2, 3);
POLYNOMIAL p5(1, 3, 5);
mAssert((p4 * p5).str(), "( 6, 19, 15 )");
mAssert((p5 * p4).str(), "( 6, 19, 15 )");
POLYNOMIAL p6(3, 1, 3, 4, -1);
POLYNOMIAL p7(2, 6, 8, -7);
mAssert((p6 / p7).str(), "( -20, 7 )");
mAssert((p6 % p7).str(), "( 169, 265 )");
mAssert((p6 << 1).str(), "( 3, 4, -1 )");
mAssert((p7 >> 2).str(), "( 0, 0, 6, 8, -7 )");
mAssert(p6.str(), "( 1, 3, 4, -1 )");
mAssert(p7.str(), "( 6, 8, -7 )");
p7 = ++p6;
mAssert(p6.str(), "( 2, 4, 5 )");
mAssert(p7.str(), "( 2, 4, 5 )");
p6 = p7--;
mAssert(p6.str(), "( 2, 4, 5 )");
mAssert(p7.str(), "( 1, 3, 4 )");
// ptr
POLYNOMIAL* pptr0 = new POLYNOMIAL;
mAssert(itostr(POLYNOMIAL::overloaded), itostr(1));
POLYNOMIAL* pptr1 = new POLYNOMIAL;
mAssert(itostr(POLYNOMIAL::overloaded), itostr(2));
mAssert(pptr0->str(), "( 0 )");
delete pptr0;
mAssert(itostr(POLYNOMIAL::overloaded), itostr(1));
delete pptr1;
mAssert(itostr(POLYNOMIAL::overloaded), itostr(0));
// relations
mAssertTrue(p6 > p7);
mAssertTrue(p6 >= p7);
mAssertTrue(!(p6 < p7));
mAssertTrue(!(p6 <= p7));
mAssertTrue(!(p6 == p7));
mAssertTrue(p6 != p7);
// shifts
mAssert((POLYNOMIAL() << 10).str(), "( 0 )");
mAssert((POLYNOMIAL() >> 10).str(), "( 0 )");
mAssert((POLYNOMIAL(0, 1) << 10).str(), "( 0 )");
mAssert((POLYNOMIAL(0, 1) >> 10).str(), "( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 )");
mAssert(((POLYNOMIAL(4, 0, 1, 2, 3, 4) >> 10) << 10).str(), "( 0, 1, 2, 3, 4 )");
mAssert(((POLYNOMIAL(4, 0, 1, 2, 3, 4) << 10) >> 10).str(), "( 0 )");
mAssert((POLYNOMIAL(4, 0, 1, 2, 3, 4) >> -10).str(), "( 0 )");
mAssert((POLYNOMIAL(4, 0, 1, 2, 3, 4) << -10).str(), "( 0 )");
mAssert(((POLYNOMIAL(4, 0, 1, 2, 3, 4) >> -10) << -10).str(), "( 0 )");
mAssert(((POLYNOMIAL(4, 0, 1, 2, 3, 4) << -10) >> -10).str(), "( 0 )");
mAssert(POLYNOMIAL(1, 0, 2).str(), "( 0, 1 )");
mAssert((POLYNOMIAL(1, 0, 2) << 1).str(), "( 1 )");
// asterisk
POLYNOMIAL p8(6, 1, 2, 3, 4, 1, 2, 7);
mAssert((p8 * POLYNOMIAL()).str(), "( 0 )");
mAssert((POLYNOMIAL() * p8).str(), "( 0 )");
mAssert((p8 * POLYNOMIAL(0, 1)).str(), "( 1, 2, 3, 4, 1, 2, 7 )");
mAssert((POLYNOMIAL(0, 1), p8).str(), "( 1, 2, 3, 4, 1, 2, 7 )");
mAssert((p8 * POLYNOMIAL(1, 0, 1)).str(), "( 0, 1, 2, 3, 4, 1, 2, 7 )");
mAssert((POLYNOMIAL(1, 0, 1) * p8).str(), "( 0, 1, 2, 3, 4, 1, 2, 7 )");
// slash percent
mAssert((POLYNOMIAL() / POLYNOMIAL(0, 1)).str(), "( 0 )");
mAssert((POLYNOMIAL() % POLYNOMIAL(0, 1)).str(), "( 0 )");
mAssert((POLYNOMIAL(2, 1, -2, 1) / POLYNOMIAL(2, 1, -2, 1)).str(), "( 1 )");
mAssert((POLYNOMIAL(2, 1, -2, 1) % POLYNOMIAL(2, 1, -2, 1)).str(), "( 0 )");
mAssert((POLYNOMIAL(0, 1) / POLYNOMIAL(0, 1)).str(), "( 1 )");
mAssert((POLYNOMIAL(0, 1) % POLYNOMIAL(0, 1)).str(), "( 0 )");
mAssert((POLYNOMIAL(4, 2, 3, 5, 7, 11) / POLYNOMIAL(0, 1)).str(), "( 2, 3, 5, 7, 11 )");
mAssert((POLYNOMIAL(4, 2, 3, 5, 7, 11) % POLYNOMIAL(0, 1)).str(), "( 0 )");
mAssert((POLYNOMIAL(4, 2, 3, 5, 7, 11) / POLYNOMIAL(1, 1, 1)).str(), "( -6, 9, -4, 11 )");
mAssert((POLYNOMIAL(4, 2, 3, 5, 7, 11) % POLYNOMIAL(1, 1, 1)).str(), "( 1 )");
mAssert((POLYNOMIAL(4, 2, 3, 5, 7, 11) / POLYNOMIAL(1, -1, -1)).str(), "( 6, -9, 4, -11 )");
mAssert((POLYNOMIAL(4, 2, 3, 5, 7, 11) % POLYNOMIAL(1, -1, -1)).str(), "( 1 )");
mAssert((POLYNOMIAL(4, 2, 3, 5, 7, 11) / POLYNOMIAL(1, 0, 1)).str(), "( 3, 5, 7, 11 )");
mAssert((POLYNOMIAL(4, 2, 3, 5, 7, 11) % POLYNOMIAL(1, 0, 1)).str(), "( 1 )");
mAssert((POLYNOMIAL(2, 1, 1, -2) / POLYNOMIAL(1, -1, 1)).str(), "( -1, -2 )");
mAssert((POLYNOMIAL(2, 1, 1, -2) % POLYNOMIAL(1, -1, 1)).str(), "( 0 )");
mAssert((POLYNOMIAL(2, 1, 1, -2) / POLYNOMIAL(0, -1)).str(), "( -1, -1, 2 )");
mAssert((POLYNOMIAL(2, 1, 1, -2) % POLYNOMIAL(0, -1)).str(), "( 0 )");
mAssert((POLYNOMIAL(2, 1, 1, 2) / POLYNOMIAL(1, -1, 1)).str(), "( 3, 2 )");
mAssert((POLYNOMIAL(2, 1, 1, 2) % POLYNOMIAL(1, -1, 1)).str(), "( 1 )");
mAssert((POLYNOMIAL(2, 1, 4, 2) / POLYNOMIAL(1, 1, 1)).str(), "( 1, 1 )");
mAssert((POLYNOMIAL(2, 1, 4, 2) % POLYNOMIAL(1, 1, 1)).str(), "( -1 )");
mAssert((POLYNOMIAL(2, 2, 3, 5) / POLYNOMIAL(1, 1, 7)).str(), "( 16, 35 )");
mAssert((POLYNOMIAL(2, 2, 3, 5) % POLYNOMIAL(1, 1, 7)).str(), "( 1 )");
mAssert((POLYNOMIAL() / POLYNOMIAL(2, 4, -4, 1)).str(), "( 0 )");
mAssert((POLYNOMIAL() % POLYNOMIAL(2, 4, -4, 1)).str(), "( 0 )");
mAssert((POLYNOMIAL(1, 1, 2) / POLYNOMIAL(2, 4, -4, 1)).str(), "( 0 )");
mAssert((POLYNOMIAL(1, 1, 2) % POLYNOMIAL(2, 4, -4, 1)).str(), "( 1, 2 )");
mAssert((POLYNOMIAL(3, -1, 4, -5, 2) / POLYNOMIAL(1, -1, 1)).str(), "( 1, -3, 2 )");
mAssert((POLYNOMIAL(3, -1, 4, -5, 2) % POLYNOMIAL(1, -1, 1)).str(), "( 0 )");
mAssert((POLYNOMIAL(3, -4, -4, 1, 1) / POLYNOMIAL(1, 1, 1)).str(), "( -4, 0, 1 )");
mAssert((POLYNOMIAL(3, -4, -4, 1, 1) % POLYNOMIAL(1, 1, 1)).str(), "( 0 )");
mAssert((POLYNOMIAL(3, -4, -4, 1, 1) / POLYNOMIAL(2, -4, 0, 1)).str(), "( 1, 1 )");
mAssert((POLYNOMIAL(3, -4, -4, 1, 1) % POLYNOMIAL(2, -4, 0, 1)).str(), "( 0 )");
POLYNOMIAL polies[] = {
POLYNOMIAL(0, 1),
POLYNOMIAL(0, -1),
POLYNOMIAL(1, 0, 1),
POLYNOMIAL(1, 0, -1),
POLYNOMIAL(1, 1, 1),
POLYNOMIAL(1, 1, -1),
POLYNOMIAL(1, -1, 1),
POLYNOMIAL(1, -1, -1),
POLYNOMIAL(2, 0, 0, 1),
POLYNOMIAL(2, 0, 1, 1),
POLYNOMIAL(2, 1, 0, 1),
POLYNOMIAL(2, 1, 1, 1),
POLYNOMIAL(2, 3, 2, 1),
POLYNOMIAL(2, 7, 0, 1),
POLYNOMIAL(2, 0, 5, 1),
POLYNOMIAL(2, -5, 0, 1)
};
int n = sizeof(polies) / sizeof(POLYNOMIAL);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
divTest(__LINE__, polies[i], polies[j]);
}
}
mAssert((POLYNOMIAL(2, 7, 7, 5) / POLYNOMIAL(1, 2, -4)).str(), "( -19, -10 )");
mAssert((POLYNOMIAL(2, 7, 7, 5) % POLYNOMIAL(1, 2, -4)).str(), "( 1 )");
mAssert((POLYNOMIAL(2, 1, 2, -1) / POLYNOMIAL(2, 2, -6, 4)).str(), "( -1 )");
mAssert((POLYNOMIAL(2, 1, 2, -1) % POLYNOMIAL(2, 2, -6, 4)).str(), "( 3, 1 )");
mAssert((POLYNOMIAL(3, -7, 5, -3, 7) / POLYNOMIAL(3, 7, 6, 4, -2)).str(), "( -1 )");
mAssert((POLYNOMIAL(3, -7, 5, -3, 7) % POLYNOMIAL(3, 7, 6, 4, -2)).str(), "( 35, 52, 22 )");
mAssert((POLYNOMIAL(4, 4, 2, 5, -2, 1) / POLYNOMIAL(2, 3, -3, -4)).str(), "( -125, 44, -16 )");
mAssert((POLYNOMIAL(4, 4, 2, 5, -2, 1) % POLYNOMIAL(2, 3, -3, -4)).str(), "( 631, -379 )");
mAssert((POLYNOMIAL(5, 1, -1, -4, -5, 0, -7) / POLYNOMIAL(1, 2, 4)).str(), "( -11, -10, -108, 56, -112 )");
mAssert((POLYNOMIAL(5, 1, -1, -4, -5, 0, -7) % POLYNOMIAL(1, 2, 4)).str(), "( 1 )");
mAssert((POLYNOMIAL(5, 4, -4, -5, 2, -7, -7) / POLYNOMIAL(1, -4, 3)).str(), "( -3712, -2541, -1602, -1323, -567 )");
mAssert((POLYNOMIAL(5, 4, -4, -5, 2, -7, -7) % POLYNOMIAL(1, -4, 3)).str(), "( -1 )");
mAssert((POLYNOMIAL(6, 0, -5, -4, 6, 1, -4, 6) / POLYNOMIAL(3, 2, -5, 6, 7)).str(), "( -4628, 4501, -3136, 2058 )");
mAssert((POLYNOMIAL(6, 0, -5, -4, 6, 1, -4, 6) % POLYNOMIAL(3, 2, -5, 6, 7)).str(), "( 9256, -44147, 46941 )");
mAssert((POLYNOMIAL(6, -1, 0, 2, 1, 3, 0, -1) / POLYNOMIAL(5, -4, 3, 1, 4, -7, 5)).str(), "( -7, -5 )");
mAssert((POLYNOMIAL(6, -1, 0, 2, 1, 3, 0, -1) % POLYNOMIAL(5, -4, 3, 1, 4, -7, 5)).str(), "( -53, 1, 72, 58, 46 )");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment