|
| 1 | +/* |
| 2 | + * InfixConverterTest.h |
| 3 | + * |
| 4 | + * Created on: Jun 2, 2017 |
| 5 | + * Author: dat |
| 6 | + */ |
| 7 | + |
| 8 | +#include <limits.h> |
| 9 | +#include "gtest/gtest.h" |
| 10 | + |
| 11 | +#include <string> |
| 12 | +#include <iostream> |
| 13 | +#include "InfixConverter.h" |
| 14 | + |
| 15 | +using namespace std; |
| 16 | + |
| 17 | +extern "C" { |
| 18 | +uint8_t addSpaces(char * buffer); |
| 19 | +} |
| 20 | + |
| 21 | +namespace |
| 22 | +{ |
| 23 | +class MyTest : public ::testing::Test |
| 24 | +{ |
| 25 | +protected: |
| 26 | + MyTest() |
| 27 | + { |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + virtual ~MyTest() |
| 32 | + { |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + virtual void SetUp() |
| 37 | + { |
| 38 | + output[0] = '\0'; |
| 39 | + } |
| 40 | + |
| 41 | + virtual void TearDown() |
| 42 | + { |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + char output[256]; |
| 47 | +}; |
| 48 | +} |
| 49 | + |
| 50 | +TEST_F(MyTest, testConverter2) |
| 51 | +{ |
| 52 | + EXPECT_EQ(InfixConverterConvertString("1+2+3", output), 0); |
| 53 | + EXPECT_STREQ(output, "1 2 + 3 +"); |
| 54 | + |
| 55 | + EXPECT_EQ(InfixConverterConvertString("1+(2*3)", output), 0); |
| 56 | + EXPECT_STREQ(output, "1 2 3 * +"); |
| 57 | +} |
| 58 | + |
| 59 | +TEST_F(MyTest, testConverter1) |
| 60 | +{ |
| 61 | + EXPECT_EQ(InfixConverterConvertString("123", output), 0); |
| 62 | + EXPECT_STREQ(output, "123"); |
| 63 | + |
| 64 | + EXPECT_EQ(InfixConverterConvertString(" 65555 ", output), 0); |
| 65 | + EXPECT_STREQ(output, "65555"); |
| 66 | + |
| 67 | + EXPECT_EQ(InfixConverterConvertString("1 + 2", output), 0); |
| 68 | + EXPECT_STREQ(output, "1 2 +"); |
| 69 | +} |
| 70 | + |
| 71 | +TEST_F(MyTest, testConverter_Success_Prior) |
| 72 | +{ |
| 73 | + EXPECT_EQ(InfixConverterConvertString("1*2+3", output), 0); |
| 74 | + EXPECT_STREQ(output, "1 2 * 3 +"); |
| 75 | + |
| 76 | + EXPECT_EQ(InfixConverterConvertString("1*2+3*4", output), 0); |
| 77 | + EXPECT_STREQ(output, "1 2 * 3 4 * +"); |
| 78 | + |
| 79 | + EXPECT_EQ(InfixConverterConvertString("1/2+3*4", output), 0); |
| 80 | + EXPECT_STREQ(output, "1 2 / 3 4 * +"); |
| 81 | + |
| 82 | + EXPECT_EQ(InfixConverterConvertString("1*2+3 *4 - 5 / 6", output), 0); |
| 83 | + EXPECT_STREQ(output, "1 2 * 3 4 * + 5 6 / -"); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_F(MyTest, testConverter_FailFormat) |
| 87 | +{ |
| 88 | + EXPECT_EQ(InfixConverterConvertString("(1", output), 1); |
| 89 | + EXPECT_EQ(InfixConverterConvertString(")1(", output), 1); |
| 90 | +} |
| 91 | + |
| 92 | +TEST_F(MyTest, testConverter_Fail_UnsupportedChar) |
| 93 | +{ |
| 94 | + EXPECT_EQ(InfixConverterConvertString("1.2", output), 2); |
| 95 | + EXPECT_EQ(InfixConverterConvertString("1^2", output), 2); |
| 96 | +} |
| 97 | + |
| 98 | +TEST_F(MyTest, testConverter_ExtraSpace) |
| 99 | +{ |
| 100 | + char input[] = " A+B"; |
| 101 | + char input2[] = "+-*/()"; |
| 102 | + |
| 103 | + EXPECT_EQ(addSpaces(input), 0); |
| 104 | + EXPECT_STREQ(input, "A + B"); |
| 105 | + |
| 106 | + EXPECT_EQ(addSpaces(input2), 0); |
| 107 | + EXPECT_STREQ(input2, "+ - * / ( )"); |
| 108 | +} |
| 109 | + |
| 110 | +TEST_F(MyTest, testConverter_1parentpair1) |
| 111 | +{ |
| 112 | + EXPECT_EQ(InfixConverterConvertString("1*(2+3)", output), 0); |
| 113 | + EXPECT_STREQ(output, "1 2 3 + *"); |
| 114 | + |
| 115 | + EXPECT_EQ(InfixConverterConvertString("(1+2)", output), 0); |
| 116 | + EXPECT_STREQ(output, "1 2 +"); |
| 117 | +} |
| 118 | + |
| 119 | +TEST_F(MyTest, testConverter_Comprehensive) |
| 120 | +{ |
| 121 | + EXPECT_EQ(InfixConverterConvertString("1* (2+3 * 4) +5", output), 0); |
| 122 | + EXPECT_STREQ(output, "1 2 3 4 * + * 5 +"); |
| 123 | + |
| 124 | + EXPECT_EQ(InfixConverterConvertString("111* (222+333 * 444) +555", output), 0); |
| 125 | + EXPECT_STREQ(output, "111 222 333 444 * + * 555 +"); |
| 126 | + |
| 127 | + EXPECT_EQ(InfixConverterConvertString("1*(2-3/4) +5/6*(7+8)", output), 0); |
| 128 | + EXPECT_STREQ(output, "1 2 3 4 / - * 5 6 / 7 8 + * +"); |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
0 commit comments