Numworks Epsilon  1.4.1
Graphing Calculator Operating System
calculation_store.cpp
Go to the documentation of this file.
1 #include <quiz.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "../calculation_store.h"
5 
6 using namespace Poincare;
7 using namespace Calculation;
8 
9 void assert_store_is(CalculationStore * store, const char * result[10]) {
10  for (int i = 0; i < store->numberOfCalculations(); i++) {
11  assert(strcmp(store->calculationAtIndex(i)->inputText(), result[i]) == 0);
12  }
13 }
14 
15 QUIZ_CASE(calculation_store) {
16  GlobalContext globalContext;
17  CalculationStore store;
18  assert(CalculationStore::k_maxNumberOfCalculations == 10);
19  for (int i = 0; i < CalculationStore::k_maxNumberOfCalculations; i++) {
20  char text[2] = {(char)(i+'0'), 0};
21  store.push(text, &globalContext);
22  assert(store.numberOfCalculations() == i+1);
23  }
24  /* Store is now {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} */
25  const char * result[10] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
26  assert_store_is(&store, result);
27 
28  store.push("10", &globalContext);
29  /* Store is now {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} */
30  const char * result1[10] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
31  assert_store_is(&store, result1);
32 
33  for (int i = 9; i > 0; i = i-2) {
34  store.deleteCalculationAtIndex(i);
35  }
36  /* Store is now {1, 3, 5, 7, 9} */
37  const char * result2[10] = {"1", "3", "5", "7", "9", "", "", "", "", ""};
38  assert_store_is(&store, result2);
39 
40  for (int i = 5; i < CalculationStore::k_maxNumberOfCalculations; i++) {
41  char text[3] = {(char)(i+'0'), 0};
42  store.push(text, &globalContext);
43  assert(store.numberOfCalculations() == i+1);
44  }
45  /* Store is now {0, 2, 4, 6, 8, 5, 6, 7, 8, 9} */
46  const char * result3[10] = {"1", "3", "5", "7", "9", "5", "6", "7", "8", "9"};
47  assert_store_is(&store, result3);
48 
49  store.deleteAll();
50  store.push("1+3/4", &globalContext);
51  store.push("ans+2/3", &globalContext);
52  ::Calculation::Calculation * lastCalculation = store.calculationAtIndex(1);
53  assert(lastCalculation->shouldDisplayApproximateOutput(&globalContext) == false);
54  assert(strcmp(lastCalculation->exactOutputText(),"29/12") == 0);
55 
56  store.push("ans+0.22", &globalContext);
57  lastCalculation = store.calculationAtIndex(2);
58  assert(lastCalculation->shouldDisplayApproximateOutput(&globalContext) == true);
59  assert(strcmp(lastCalculation->approximateOutputText(),"2.6366666666667") == 0);
60 }
void assert_store_is(CalculationStore *store, const char *result[10])
Calculation * push(const char *text, Poincare::Context *context)
Calculation * calculationAtIndex(int i)
#define assert(e)
Definition: assert.h:9
const char * exactOutputText()
Definition: calculation.cpp:84
const char * approximateOutputText()
Definition: calculation.cpp:88
const char * inputText()
Definition: calculation.cpp:80
bool shouldDisplayApproximateOutput(Poincare::Context *context)
QUIZ_CASE(calculation_store)
int strcmp(const char *s1, const char *s2)
Definition: strcmp.c:3