Guest User

TestConstructor.cpp

a guest
Oct 2nd, 2014
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include "BigInteger.h"
  2. #include "BigIntegerException.h"
  3. #include "gtest\gtest.h"
  4.  
  5. /**
  6. *   To test BigInteger constructors using toString function
  7. *   9 cases:
  8. *       1. default constructor
  9. *       2. constructor with a VALID short string argument
  10. *       3. constructor with a VALID long string argument
  11. *       (within long long range)
  12. *       4. constructor with a VALID long string argument
  13. *       (out of long long range)
  14. *       5. constructor with an INVALID short string - exception
  15. *       6. constructor with an INVALID long string - exception
  16. *       7. constructor with an empty string - exception
  17. *       8. constructor with leading 0
  18. *       9. copy constructor
  19. */
  20.  
  21. /**
  22. *   Case 1
  23. */
  24. TEST(TestConstructor, defaultConstructor) {
  25.     BigInteger bigInt1;
  26.     EXPECT_EQ("0", bigInt1.toString());
  27. }
  28.  
  29. /**
  30. *   Case 2, 3, 4
  31. */
  32. TEST(TestConstructor, constructorWithValidString) {
  33.     // case 2
  34.     BigInteger bigInt1("123");
  35.     EXPECT_EQ("123", bigInt1.toString());
  36.  
  37.     // case 3
  38.     BigInteger bigInt2("1234567890123456789");
  39.     EXPECT_EQ("1234567890123456789", bigInt2.toString());
  40.  
  41.     // case 4
  42.     BigInteger bigInt3("123456789012345678901");
  43.     EXPECT_EQ("123456789012345678901", bigInt3.toString());
  44. }
  45.  
  46. /**
  47. *   Case 5, 6, 7:
  48. *   (a): derived class
  49. *   (b): base class
  50. *
  51. * Notes:
  52. *   Because BigIntegerException is base class of NumberFormatException,
  53. *   same error should be able to catch by base class
  54. */
  55. TEST(TestConstructor, constructorWithInvalidString) {
  56.     // case 5a
  57.     EXPECT_THROW(BigInteger bigInt5("123a"), NumberFormatException);
  58.  
  59.     // case 5b
  60.     EXPECT_THROW(BigInteger bigInt5("123a"), BigIntegerException);
  61.  
  62.     // case 6a
  63.     EXPECT_THROW(
  64.         BigInteger bigInt5("123456789012345678901234567890a"),
  65.         NumberFormatException);
  66.  
  67.     // case 6b
  68.     EXPECT_THROW(
  69.         BigInteger bigInt5("123456789012345678901234567890a"),
  70.         BigIntegerException);
  71.  
  72.     // case 7
  73.     EXPECT_THROW(
  74.         BigInteger bigInt5(""),
  75.         BigIntegerException);
  76. }
  77.  
  78. /**
  79. *   Case 8
  80. */
  81. TEST(TestConstructor, constructorWithLeadingZero) {
  82.     BigInteger bigInt("000");
  83.     EXPECT_EQ("0", bigInt.toString());
  84.  
  85.     bigInt = BigInteger("0001");
  86.     EXPECT_EQ("1", bigInt.toString());
  87. }
  88.  
  89. /**
  90. *   Case 9
  91. */
  92. TEST(TestConstructor, copyConstructor) {
  93.     BigInteger bigInt1("123");
  94.  
  95.     BigInteger bigInt2(bigInt1);
  96.     EXPECT_EQ("123", bigInt2.toString());
  97.  
  98.     bigInt1 = BigInteger("456");
  99.     EXPECT_EQ("123", bigInt2.toString());
  100. }
Advertisement
Add Comment
Please, Sign In to add comment