calc/examples/test.cpp

93 lines
2.7 KiB
C++
Raw Permalink Normal View History

2024-03-08 15:27:04 +01:00
#include "../src/include/parser.hpp"
#include <assert.h>
#include <filesystem>
#include <iostream>
int main() {
{
std::cout << "running testset 1\n";
CParser a;
a.convert("123456789 + 987654321");
assert( a.m_output == "1111111110" );
a.convert("(12/50 + 1/8)/4 + 2/7");
assert( a.m_output == "2111 / 5600" );
a.convert("velkeCislo=100000000000000000000000000000000000000000000");
assert( a.m_output == "100000000000000000000000000000000000000000000");
a.convert("$velkeCislo*$velkeCislo");
assert( a.m_output == "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" );
a.convert("$ans / $velkeCislo");
assert( a.m_output == "100000000000000000000000000000000000000000000" );
a.convert("!save sav");
CParser b;
b.convert("!load sav");
b.convert("$velkeCislo");
assert( b.m_output == "100000000000000000000000000000000000000000000" );
std::filesystem::path pth = std::filesystem::current_path();
std::filesystem::remove( pth / "sav" );
b.convert("!load sav");
assert( b.m_output == "load failed" );
b.convert("1 )+ 2");
assert( b.m_output == "bad parenthesis parity" );
b.convert("1 + ()");
assert( b.m_output == "Caught exception: parsing failed" );
b.convert(".3");
assert( b.m_output == "3 / 10" );
b.convert( "(.1 + 9.9) % (3 * 2)" );
assert( b.m_output == "4" );
}
{
std::cout << "running testset 2\n";
CParser a;
// Test case 1: Complex arithmetic expression
a.convert("((3.14 + 2) * 5 - 8) / 2");
assert(a.m_output == "177 / 20");
// Test case 2: Division by zero
a.convert("1 / 0");
assert(a.m_output == "Caught exception: division by zero");
// Test case 3: Mixed fraction and decimal calculations
a.convert("(1/4 + 0.75) * 2");
assert(a.m_output == "2");
// Test case 4: Negative numbers and subtraction
a.convert("-5 + 7 - (-2) * (-3)");
assert(a.m_output == "-4");
// Test case 5: Exponentiation
a.convert("(2/1) ^ 16");
assert(a.m_output == "65536");
// Test case 6: Order of operations
a.convert("2 * 3 + 4 / 2 - 1");
assert(a.m_output == "7");
// Test case 7: Large numbers
a.convert("1234567890 + 9876543210");
assert(a.m_output == "11111111100");
// Test case 8: Error handling for invalid input
a.convert("1 + 2 *");
assert(a.m_output == "Caught exception: parsing failed");
// Test case 9: Variable assignment and retrieval
a.convert("x = 5");
assert(a.m_output == "5");
a.convert("$x + 3");
assert(a.m_output == "8");
}
std::cout << "all tests successful!" << std::endl;
return 0;
}