Data structure and algorithm

profileAsutosh
assg03-tests.cpp

/** @file assg03-tests.cpp * @brief Unit tests for Assignment 03, practice with classes and dynamic * memory allocation. * * @author Jane Programmer * @note cwid : 123 45 678 * @note class: COSC 2336, Summer 2020 * @note ide : Atom Text Editor 1.46.0 / build package / GNU gcc tools * @note assg : Assignment 03 * @date June 1, 2020 * * Practice with classes and dynamic memory allocation. In this * assignment * we build a class to represent a large integer. We use * dynamic memory * allocation to manage an array of digits for the * LargeInteger object. * This file is a set of unit tests of the LargeInteger * class using the * catch2 unit test framework. */ #include <iostream> #define CATCH_CONFIG_MAIN #include "catch.hpp" #include "LargeInteger.hpp" using namespace std; /** test default constructor and tostring() implementation * Uncomment the following test case block and write your code to pass * the tests. You were given a default and standard constructor for * the class. You need to implement the tostring() method, which we * will use extensively in the tests to test your other member * functions. */ /* uncomment the tests cases 1 at a time. This test case tests implementation * of the tostring() member function which we use for further testing. TEST_CASE("<tostring()> member function tests using default and standard constructor", "[tostring]") { LargeInteger li1; CHECK( li1.tostring() == "0" ); LargeInteger li2(12345); CHECK( li2.tostring() == "12345" ); // an even larger value LargeInteger li3(1234567890); CHECK( li3.tostring() == "1234567890" ); } */ /** test array constructor * Uncomment the following test case block and write your code to pass * the tests. */ /* uncomment this test case when ready to implement and test the * array constructor you are to create TEST_CASE("<array constructor> constructor function tests", "[array constructor]") { // a kind of large integer int digits1[] = {8, 4, 6, 3, 8, 4, 7, 4, 1, 2}; LargeInteger li1(10, digits1); CHECK( li1.tostring() == "2147483648" ); // a really large integer int digits2[] = {2, 4, 7, 3, 6, 5, 4, 3, 5, 9, 2, 7, 3, 8, 4, 9, 6, 2, 1, 0, 7, 6, 1, 9, 3, 4, 2, 0, 2, 8}; LargeInteger li2(30, digits2); CHECK( li2.tostring() == "820243916701269483729534563742" ); } */ // define a diverse set of large integer instances that we can // reuse in all of the following test cases /* When your array constructor is working you should uncomment these * declarations of LargeInteger instances which are used in all of the * following test cases LargeInteger li1; // 1 digits LargeInteger li2(5); // 1 digit LargeInteger li3(34567); // 5 digits int digits4[] = {9, 8, 7, 6, 5}; LargeInteger li4(5, digits4); // 5 digits LargeInteger li5(398298312); // 9 digits int digits6[] = {3, 3, 1, 4, 2, 1, 5, 1, 2, 4, 7, 6, 9, 3, 9, 5, 6}; LargeInteger li6(17, digits6); // 17 digits */ /** test maxDigits() member function * Uncomment the following test case block and write your code to pass * the tests. */ /* uncomment this test case to work on implementation of maxDigits() * member function TEST_CASE("<maxDigits()> member function tests", "[maxDigits]") { // when max digits are equal CHECK( li1.maxDigits(li2) == 1 ); CHECK( li2.maxDigits(li1) == 1 ); CHECK( li3.maxDigits(li4) == 5 ); CHECK( li4.maxDigits(li3) == 5 ); // not equal CHECK( li1.maxDigits(li3) == 5 ); CHECK( li4.maxDigits(li2) == 5 ); // some more checks CHECK( li5.maxDigits(li6) == 17 ); CHECK( li6.maxDigits(li5) == 17 ); CHECK( li5.maxDigits(li3) == 9 ); CHECK( li3.maxDigits(li6) == 17 ); CHECK( li5.maxDigits(li2) == 9 ); CHECK( li1.maxDigits(li6) == 17 ); } */ /** test digitAtPlace() member function * Uncomment the following test case block and write your code to pass * the tests. */ /* uncomment this test case to work on implementation of digitAtPlace() * member function TEST_CASE("<digitAtPlace()> member function tests (reading values)", "[digitAtPlace]") { // test single digit access working CHECK( li1.digitAtPlace(0) == 0 ); CHECK( li2.digitAtPlace(0) == 5 ); // check begin and end bounds CHECK( li3.digitAtPlace(0) == 7 ); CHECK( li3.digitAtPlace(4) == 3 ); CHECK( li5.digitAtPlace(0) == 2 ); CHECK( li5.digitAtPlace(8) == 3 ); CHECK( li6.digitAtPlace(0) == 3 ); CHECK( li6.digitAtPlace(16) == 6 ); // check some aribitrary values not at ends CHECK( li3.digitAtPlace(2) == 5 ); CHECK( li4.digitAtPlace(1) == 8 ); CHECK( li5.digitAtPlace(6) == 8 ); CHECK( li5.digitAtPlace(2) == 3 ); CHECK( li6.digitAtPlace(4) == 2 ); CHECK( li6.digitAtPlace(14) == 9 ); // according to specifications, illegal indexes should give a 0 as the digit at place CHECK( li1.digitAtPlace(-1) == 0 ); CHECK( li2.digitAtPlace(-5) == 0 ); CHECK( li4.digitAtPlace(-7) == 0 ); CHECK( li1.digitAtPlace(1) == 0 ); CHECK( li2.digitAtPlace(5) == 0 ); CHECK( li3.digitAtPlace(6) == 0 ); CHECK( li4.digitAtPlace(6) == 0 ); CHECK( li5.digitAtPlace(15) == 0 ); CHECK( li6.digitAtPlace(17) == 0 ); } */ /** test appendDigit() member function * Uncomment the following test case block and write your code to pass * the tests. */ /* uncomment this test case to work on implementation of appendDigit() * member function TEST_CASE("<appendDigit()> member function tests", "[appendDigit]") { li2.appendDigit(7); CHECK( li2.tostring() == "75" ); li3.appendDigit(9); CHECK( li3.tostring() == "934567" ); li5.appendDigit(1); CHECK( li5.tostring() == "1398298312" ); li6.appendDigit(7); CHECK( li6.tostring() == "765939674215124133" ); // append of 0 (as most significant digit) should be ignored li1.appendDigit(0); CHECK( li1.tostring() == "0" ); li4.appendDigit(0); CHECK( li4.tostring() == "56789" ); li5.appendDigit(0); CHECK( li5.tostring() == "1398298312" ); li6.appendDigit(0); CHECK( li6.tostring() == "765939674215124133" ); } */ /** test add() member function * Uncomment the following test case block and write your code to pass * the tests. */ /* uncomment this test case to work on implementation of add() member function TEST_CASE("<add()> member function tests", "[add]") { LargeInteger lires; lires = li1.add(li2); CHECK( lires.tostring() == "75" ); lires = li2.add(li1); CHECK( lires.tostring() == "75" ); lires = li3.add(li4); CHECK( lires.tostring() == "991356" ); lires = li4.add(li3); CHECK( lires.tostring() == "991356" ); lires = li5.add(li6); CHECK( lires.tostring() == "765939675613422445" ); lires = li6.add(li5); CHECK( lires.tostring() == "765939675613422445" ); // an explicit test of carry on last digit LargeInteger li7(999999999); int digits8[] = {9, 9, 9, 9, 9, 9, 9, 9, 9}; LargeInteger li8(9, digits8); lires = li7.add(li8); CHECK( lires.tostring() == "1999999998" ); lires = li8.add(li7); CHECK( lires.tostring() == "1999999998" ); } */