Skip to content

Commit 39ec309

Browse files
committed
Simplifying tests
1 parent d4aee6c commit 39ec309

File tree

1 file changed

+22
-45
lines changed

1 file changed

+22
-45
lines changed

tests/CalculatorTest.php

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,52 +35,21 @@ public function testGetToken() {
3535
}
3636

3737
public function testGetReversedPolishNotation() {
38-
$this->_calculator->setExpression('1+2');
39-
$queue = $this->_calculator->getReversePolishNotation($this->_calculator->getTokens());
40-
$this->assertEquals('12+', $this->queueToString($queue));
41-
42-
$this->_calculator->setExpression('1^-2');
43-
$queue = $this->_calculator->getReversePolishNotation($this->_calculator->getTokens());
44-
$this->assertEquals('1-2^', $this->queueToString($queue));
45-
46-
$this->_calculator->setExpression('1^(-2)');
47-
$queue = $this->_calculator->getReversePolishNotation($this->_calculator->getTokens());
48-
$this->assertEquals('1-2^', $this->queueToString($queue));
49-
50-
$this->_calculator->setExpression('1+2-3');
51-
$queue = $this->_calculator->getReversePolishNotation($this->_calculator->getTokens());
52-
$this->assertEquals('12+3-', $this->queueToString($queue));
53-
54-
$this->_calculator->setExpression('3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3');
55-
$queue = $this->_calculator->getReversePolishNotation($this->_calculator->getTokens());
56-
$this->assertEquals('342*15-23^^/+', $this->queueToString($queue));
57-
58-
$this->_calculator->setExpression('3 + 4 * 2 / ( 1 - 5 )');
59-
$queue = $this->_calculator->getReversePolishNotation($this->_calculator->getTokens());
60-
$this->assertEquals('342*15-/+', $this->queueToString($queue));
61-
62-
$this->_calculator->setExpression('5 + ((1 + 2) * 4) - 3');
63-
$queue = $this->_calculator->getReversePolishNotation($this->_calculator->getTokens());
64-
$this->assertEquals('512+4*+3-', $this->queueToString($queue));
38+
$this->executeGetReversePolishNotation('1+2', '12+');
39+
$this->executeGetReversePolishNotation('1^-2', '1-2^');
40+
$this->executeGetReversePolishNotation('1^(-2)', '1-2^');
41+
$this->executeGetReversePolishNotation('1+2-3', '12+3-');
42+
$this->executeGetReversePolishNotation('3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3', '342*15-23^^/+');
43+
$this->executeGetReversePolishNotation('3 + 4 * 2 / ( 1 - 5 )', '342*15-/+');
44+
$this->executeGetReversePolishNotation('5 + ((1 + 2) * 4) - 3', '512+4*+3-');
6545
}
6646

6747
public function testCalculate() {
68-
$limit = 5;
69-
70-
$this->_calculator->setExpression('250*14.3');
71-
$this->assertEquals(3575, $this->_calculator->calculate($limit));
72-
73-
$this->_calculator->setExpression('3^6 / 117');
74-
$this->assertEquals(6.23077, $this->_calculator->calculate($limit));
75-
76-
$this->_calculator->setExpression('(2.16 - 48.34)^-1');
77-
$this->assertEquals(-0.02165, $this->_calculator->calculate($limit));
78-
79-
$this->_calculator->setExpression('(59 - 15 + 3*6)/21');
80-
$this->assertEquals(2.95238, $this->_calculator->calculate($limit));
81-
82-
$this->_calculator->setExpression('-(-5)');
83-
$this->assertEquals('5', $this->_calculator->calculate($limit));
48+
$this->executeCalculation('250*14.3', 3575);
49+
$this->executeCalculation('3^6 / 117', 6.23077);
50+
$this->executeCalculation('(2.16 - 48.34)^-1', -0.02165);
51+
$this->executeCalculation('(59 - 15 + 3*6)/21', 2.95238);
52+
$this->executeCalculation('3-(4-6)', 5);
8453
}
8554

8655
public function testFormatNumber() {
@@ -95,12 +64,20 @@ public function testFormatNumber() {
9564
$this->assertEquals('12345.1', $this->_calculator->formatNumber(12345.100001, $limit));
9665
}
9766

98-
private function queueToString(\SplQueue $queue) {
67+
private function executeCalculation($expression, $expect, $limit = 5) {
68+
$this->_calculator->setExpression($expression);
69+
$this->assertEquals($expect, $this->_calculator->calculate($limit));
70+
}
71+
72+
private function executeGetReversePolishNotation($expression, $expect) {
73+
$this->_calculator->setExpression($expression);
74+
$queue = $this->_calculator->getReversePolishNotation($this->_calculator->getTokens());
75+
9976
$result = '';
10077
while($queue->count() > 0)
10178
$result .= $queue->dequeue();
10279

103-
return $result;
80+
$this->assertEquals($expect, $result);
10481
}
10582
}
10683

0 commit comments

Comments
 (0)