EMath++
Classes for mathematical concepts
Loading...
Searching...
No Matches
eparser.cpp
Go to the documentation of this file.
1
8#include <gtest/gtest.h>
9#include "eparser.h"
14class EParserTest : public ::testing::Test {
15 public:
20 EParserTest() : lexer(""), parser("") {};
25 void set_input(const std::string &input) {
26 this->lexer = emthp::Lexer(input);
27 this->parser = emthp::Parser(input);
28 }
29 protected:
38};
43 emthp::Token token;
44 this->set_input("2x^2 - 8");
45
46 token = this->lexer.next();
47 ASSERT_EQ(token.type, emthp::TokenType::T_NUMBER);
48 ASSERT_EQ(token.value, 2.0);
49
50 token = this->lexer.next();
51 ASSERT_EQ(token.type, emthp::TokenType::T_VARIABLE);
52 ASSERT_EQ(token.value, 0.0);
53
54 token = this->lexer.next();
55 ASSERT_EQ(token.type, emthp::TokenType::T_EXPONENT);
56 ASSERT_EQ(token.value, 0.0);
57
58 token = this->lexer.next();
59 ASSERT_EQ(token.type, emthp::TokenType::T_NUMBER);
60 ASSERT_EQ(token.value, 2.0);
61
62 token = this->lexer.next();
63 ASSERT_EQ(token.type, emthp::TokenType::T_MINUS);
64 ASSERT_EQ(token.value, 0.0);
65
66 token = this->lexer.next();
67 ASSERT_EQ(token.type, emthp::TokenType::T_NUMBER);
68 ASSERT_EQ(token.value, 8.0);
69
70 token = this->lexer.next();
71 ASSERT_EQ(token.type, emthp::TokenType::T_EOF);
72 ASSERT_EQ(token.value, 0.0);
73}
77TEST_F(EParserTest, CompoundLexer) {
78 emthp::Token token;
79 this->set_input("1.11x^2.22");
80
81 token = this->lexer.next();
82 ASSERT_EQ(token.type, emthp::TokenType::T_NUMBER);
83 ASSERT_EQ(token.value, 1.11);
84
85 token = this->lexer.next();
86 ASSERT_EQ(token.type, emthp::TokenType::T_VARIABLE);
87 ASSERT_EQ(token.value, 0.0);
88
89 token = this->lexer.next();
90 ASSERT_EQ(token.type, emthp::TokenType::T_EXPONENT);
91 ASSERT_EQ(token.value, 0.0);
92
93 token = this->lexer.next();
94 ASSERT_EQ(token.type, emthp::TokenType::T_NUMBER);
95 ASSERT_EQ(token.value, 2.22);
96
97 token = this->lexer.next();
98 ASSERT_EQ(token.type, emthp::TokenType::T_EOF);
99 ASSERT_EQ(token.value, 0.0);
100}
104TEST_F(EParserTest, ArithmeticLexer) {
105 emthp::Token token;
106 this->set_input("2*8/4^3 x^ 2^2");
107
108 token = this->lexer.next();
109 ASSERT_EQ(token.type, emthp::TokenType::T_NUMBER);
110 ASSERT_EQ(token.value, 64.0);
111
112 token = this->lexer.next();
113 ASSERT_EQ(token.type, emthp::TokenType::T_VARIABLE);
114 ASSERT_EQ(token.value, 0.0);
115
116 token = this->lexer.next();
117 ASSERT_EQ(token.type, emthp::TokenType::T_EXPONENT);
118 ASSERT_EQ(token.value, 0.0);
119
120 token = this->lexer.next();
121 ASSERT_EQ(token.type, emthp::TokenType::T_NUMBER);
122 ASSERT_EQ(token.value, 4.0);
123
124 token = this->lexer.next();
125 ASSERT_EQ(token.type, emthp::TokenType::T_EOF);
126 ASSERT_EQ(token.value, 0.0);
127}
131TEST_F(EParserTest, Variables) {
132 this->set_input("x"); ASSERT_EQ(this->parser.parse_monomial(), emth::Monomial(1, 1));
133 this->set_input("2x"); ASSERT_EQ(this->parser.parse_monomial(), emth::Monomial(2, 1));
134 this->set_input("x^2"); ASSERT_EQ(this->parser.parse_monomial(), emth::Monomial(1, 2));
135 this->set_input("2x^2"); ASSERT_EQ(this->parser.parse_monomial(), emth::Monomial(2, 2));
136}
140TEST_F(EParserTest, Compound) {this->set_input("2x^2 - 2x + 2"); ASSERT_EQ(this->parser.parse_polynomial(), emth::Polynomial({emth::Monomial(2, 2), emth::Monomial(-2, 1), emth::Monomial(2, 0)}));}
145 this->set_input("-x"); ASSERT_EQ(this->parser.parse_monomial(), emth::Monomial(-1, 1));
146 this->set_input("--x"); ASSERT_EQ(this->parser.parse_monomial(), emth::Monomial(1, 1));
147 this->set_input("---x"); ASSERT_EQ(this->parser.parse_monomial(), emth::Monomial(-1, 1));
148 this->set_input("-+x"); ASSERT_EQ(this->parser.parse_monomial(), emth::Monomial(-1, 1));
149 this->set_input("+-+x"); ASSERT_EQ(this->parser.parse_monomial(), emth::Monomial(-1, 1));
150}
155 this->set_input("(1)<···(0)<···_x_···>^{2}"); ASSERT_EQ(this->parser.parse_monomial(), emth::Monomial(10, 2));
156 this->set_input("(1)<···(0)<···_x_···>^{2} - (0)<···(5)<···_x_···>^{2}"); ASSERT_EQ(this->parser.parse_polynomial(), emth::Polynomial({emth::Monomial(5, 2)}));
157}
161TEST_F(EParserTest, Edge) {this->set_input(""); ASSERT_EQ(this->parser.parse_polynomial(), emth::Polynomial());}
169int main(int argc, char** argv) {testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();}
Class that provides a test fixture for the test cases.
Definition: eparser.cpp:14
emthp::Parser parser
The parser object on which to perform the tests on.
Definition: eparser.cpp:37
void set_input(const std::string &input)
Function for setting the input to the lexer and parser objects.
Definition: eparser.cpp:25
emthp::Lexer lexer
The lexer object on which to perform the tests on.
Definition: eparser.cpp:33
EParserTest()
Standalone constructor for the EParserTest class.
Definition: eparser.cpp:20
Class for representing and operating monomials.
Definition: emath.h:187
Class for representing and operating polynomials.
Definition: emath.h:392
Class that works as a token supplier for the Parser class.
Definition: eparser.h:70
Class that wraps a std::string and parses it as a Monomial or as a Polynomial.
Definition: eparser.h:125
This is the main header file for the equation parser.
int main()
The main function that creates the efc::Application instance, runs it and when it finishes it deletes...
Definition: interface.cpp:1097
@ T_EOF
EOF type.
Definition: eparser.h:48
@ T_MINUS
Minus sign type.
Definition: eparser.h:32
@ T_VARIABLE
X type.
Definition: eparser.h:40
@ T_EXPONENT
Exponent sign type.
Definition: eparser.h:44
@ T_NUMBER
Number type.
Definition: eparser.h:36
Struct for representing a token as a type and a value.
Definition: eparser.h:54
double value
The token's numerical value as a double.
Definition: eparser.h:63
TokenType type
The token's type as a TokenType.
Definition: eparser.h:59
TEST_F(EParserTest, Lexer)
Definition: eparser.cpp:42