Numworks Epsilon  1.4.1
Graphing Calculator Operating System
convert_expression_to_text.cpp
Go to the documentation of this file.
1 #include <quiz.h>
2 #include <poincare.h>
3 #include <string.h>
4 #include <ion.h>
5 #include <stdlib.h>
6 #include <assert.h>
7 #include <cmath>
8 #include "helper.h"
9 
10 using namespace Poincare;
15 
16 template<typename T>
17 void assert_float_prints_to(T a, const char * result, PrintFloat::Mode mode = ScientificDisplay, int significantDigits = 7, int bufferSize = 250) {
18  quiz_print(result);
19 
20  int tagSize = 8;
21  unsigned char tag = 'O';
22  char * taggedBuffer = new char[bufferSize+2*tagSize];
23  memset(taggedBuffer, tag, bufferSize+2*tagSize);
24  char * buffer = taggedBuffer + tagSize;
25 
26  PrintFloat::convertFloatToText<T>(a, buffer, bufferSize, significantDigits, mode);
27 
28  for (int i=0; i<tagSize; i++) {
29  assert(taggedBuffer[i] == tag);
30  }
31  for (int i=tagSize+strlen(buffer)+1; i<bufferSize+2*tagSize; i++) {
32  assert(taggedBuffer[i] == tag);
33  }
35 
36  assert(strcmp(buffer, result) == 0);
37 
38  delete[] taggedBuffer;
39 }
40 
41 void assert_expression_prints_to(Expression * e, const char * result, PrintFloat::Mode mode = ScientificDisplay, Expression::ComplexFormat format = Cartesian, int bufferSize = 250) {
42  quiz_print(result);
43 
44  int tagSize = 8;
45  unsigned char tag = 'O';
46  char * taggedBuffer = new char[bufferSize+2*tagSize];
47  memset(taggedBuffer, tag, bufferSize+2*tagSize);
48  char * buffer = taggedBuffer + tagSize;
49 
52  e->writeTextInBuffer(buffer, bufferSize);
54 
55  for (int i=0; i<tagSize; i++) {
56  assert(taggedBuffer[i] == tag || taggedBuffer[i] == 0);
57  }
58  for (int i=tagSize+strlen(buffer)+1; i<bufferSize+2*tagSize; i++) {
59  assert(taggedBuffer[i] == tag || taggedBuffer[i] == 0);
60  }
61 
62  assert(strcmp(buffer, result) == 0);
63 
64  delete[] taggedBuffer;
65 }
66 
68  /* We expect 7 significative numbers but do not display 0 */
69  assert_float_prints_to(123.456f, "1.23456E2");
70  assert_float_prints_to(1.234567891011, "1.234568");
71  assert_float_prints_to(2.0f, "2");
72  assert_float_prints_to(123456789.0, "1.234568E8");
73  assert_float_prints_to(0.00000123456789f, "1.234568E-6");
74  assert_float_prints_to(0.99, "9.9E-1");
75  assert_float_prints_to(-123.456789f, "-1.234568E2");
76  assert_float_prints_to(-0.000123456789, "-1.234568E-4");
77  assert_float_prints_to(0.0f, "0");
78  assert_float_prints_to(10000000000000000000000000000.0, "1E28");
79  /* Converting 10000000000000000000000000000.0f into a decimal display would
80  * overflow the number of significant digits set to 7. When this is the case, the
81  * display mode is automatically set to scientific. */
82  assert_float_prints_to(10000000000000000000000000000.0, "1E28", DecimalDisplay);
83  assert_float_prints_to(1000000.0, "1000000", DecimalDisplay);
84  assert_float_prints_to(10000000.0f, "1E7", DecimalDisplay);
85  assert_float_prints_to(0.000001, "0.000001", DecimalDisplay);
86  /* Converting 0.00000001f into a decimal display would also overflow the
87  * number of significant digits set to 7. */
88  assert_float_prints_to(0.0000001f, "1E-7", DecimalDisplay);
89  assert_float_prints_to(-0.000000000000000000000000000000009090018, "-9.090018E-33");
90  assert_float_prints_to(123.421f, "123.4", DecimalDisplay, 4, 6);
91  assert_float_prints_to(123.421, "1.2E2", DecimalDisplay, 5, 6);
92  assert_float_prints_to(9.999999f, "10", DecimalDisplay, 6);
93  assert_float_prints_to(-9.99999904, "-10", DecimalDisplay, 6);
94  assert_float_prints_to(-0.017452, "-0.01745", DecimalDisplay, 6);
95  assert_float_prints_to(1E50, "1E50", DecimalDisplay, 9);
96  assert_float_prints_to(100.0, "100", DecimalDisplay, 9);
97  assert_float_prints_to(12345.678910121314, "12345.678910121", DecimalDisplay, 14);
98  assert_float_prints_to(9.999999999999999999999E12, "1E13", DecimalDisplay, 9);
99  assert_float_prints_to(-0.000000099999999, "-0.0000001", DecimalDisplay, 9);
100 }
101 
102 QUIZ_CASE(poincare_rational_to_text) {
103  Rational r0(2,3);
104  assert_expression_prints_to(&r0, "2/3");
105  Rational r1("12345678910111213","123456789101112131");
106  assert_expression_prints_to(&r1, "12345678910111213/123456789101112131");
107  Rational r2("123456789112345678921234567893123456789412345678951234567896123456789612345678971234567898123456789912345678901234567891123456789212345678931234567894123456789512345678961234567896123456789712345678981234567899123456789","1");
108  assert_expression_prints_to(&r2, "123456789112345678921234567893123456789412345678951234567896123456789612345678971234567898123456789912345678901234567891123456789212345678931234567894123456789512345678961234567896123456789712345678981234567899123456789");
109 }
110 
111 QUIZ_CASE(poincare_decimal_to_text) {
112  Decimal d0(Integer("-123456789"),30);
113  assert_expression_prints_to(&d0, "-1.23456789E30");
114  Decimal d1(Integer("123456789"),30);
115  assert_expression_prints_to(&d1, "1.23456789E30");
116  Decimal d2(Integer("-123456789"),-30);
117  assert_expression_prints_to(&d2, "-1.23456789E-30");
118  Decimal d3(Integer("-12345"),-3);
119  assert_expression_prints_to(&d3, "-0.0012345");
120  Decimal d4(Integer("12345"),-3);
121  assert_expression_prints_to(&d4, "0.0012345");
122  Decimal d5(Integer("12345"),3);
123  assert_expression_prints_to(&d5, "1234.5");
124  Decimal d6(Integer("-12345"),3);
125  assert_expression_prints_to(&d6, "-1234.5");
126  Decimal d7(Integer("12345"),6);
127  assert_expression_prints_to(&d7, "1234500");
128  Decimal d8(Integer("-12345"),6);
129  assert_expression_prints_to(&d8, "-1234500");
130  Decimal d9(Integer("-12345"),-1);
131  assert_expression_prints_to(&d9, "-0.12345");
132  Decimal d10(Integer("12345"),-1);
133  assert_expression_prints_to(&d10, "0.12345");
134 
135  Decimal e0(-1.23456789E30);
136  assert_expression_prints_to(&e0, "-1.23456789E30");
137  Decimal e1(1.23456789E30);
138  assert_expression_prints_to(&e1, "1.23456789E30");
139  Decimal e2(-1.23456789E-30);
140  assert_expression_prints_to(&e2, "-1.23456789E-30");
141  Decimal e3(-1.2345E-3);
142  assert_expression_prints_to(&e3, "-0.0012345");
143  Decimal e4(1.2345E-3);
144  assert_expression_prints_to(&e4, "0.0012345");
145  Decimal e5(1.2345E3);
146  assert_expression_prints_to(&e5, "1234.5");
147  Decimal e6(-1.2345E3);
148  assert_expression_prints_to(&e6, "-1234.5");
149  Decimal e7(1.2345E6);
150  assert_expression_prints_to(&e7, "1234500");
151  Decimal e8(-1.2345E6);
152  assert_expression_prints_to(&e8, "-1234500");
153  Decimal e9(-1.2345E-1);
154  assert_expression_prints_to(&e9, "-0.12345");
155  Decimal e10(1.2345E-1);
156  assert_expression_prints_to(&e10, "0.12345");
157  Decimal e11(1);
158  assert_expression_prints_to(&e11, "1");
159  Decimal e12(0.9999999999999995);
160  assert_expression_prints_to(&e12, "1");
161  Decimal e13(0.999999999999995);
162  assert_expression_prints_to(&e13, "9.99999999999995E-1");
163  Decimal e14(0.000000999999999999995);
164  assert_expression_prints_to(&e14, "9.99999999999995E-7");
165  Decimal e15(0.0000009999999999999995);
166  assert_expression_prints_to(&e15, "0.000001");
167  Decimal e16(0.0000009999999999901200121020102010201201201021099995);
168  assert_expression_prints_to(&e16, "9.9999999999012E-7");
169 }
170 
171 QUIZ_CASE(poincare_complex_to_text) {
175  assert_expression_prints_to(&c1, "2.2360679774998*X^(1.1071487177941*I)", DecimalDisplay, Polar);
179  assert_expression_prints_to(&c3, "2.7682369840749*X^(2.0596486811226*I)", DecimalDisplay, Polar);
180  Complex<double> c4 = Complex<double>::Cartesian(-1.3, -2.444);
182  Complex<double> c5 = Complex<double>::Cartesian(64078208.0, 119229408.0);
183  assert_expression_prints_to(&c5, "64078208+119229408*I", DecimalDisplay, Cartesian);
184  Complex<double> c6 = Complex<double>::Cartesian(64078208.0, 119229408.0);
185  assert_expression_prints_to(&c6, "135357557.86997*X^(1.0776501182461*I)", DecimalDisplay, Polar);
188  Complex<float> c8 = Complex<float>::Cartesian(0.0f, 0.0f);
196 
197 }
#define NAN
Definition: math.h:30
void translate_in_ASCII_chars(char *expression)
Definition: helper.cpp:30
static Preferences * sharedPreferences()
Definition: preferences.cpp:14
void * memset(void *b, int c, size_t len)
Definition: memset.c:3
static Complex< T > Cartesian(T a, T b)
Definition: complex.cpp:28
#define assert(e)
Definition: assert.h:9
QUIZ_CASE(assert_float_prints_to)
#define T(x)
Definition: events.cpp:26
void assert_float_prints_to(T a, const char *result, PrintFloat::Mode mode=ScientificDisplay, int significantDigits=7, int bufferSize=250)
constexpr PrintFloat::Mode ScientificDisplay
constexpr Expression::ComplexFormat Cartesian
void setComplexFormat(Expression::ComplexFormat complexFormat)
Definition: preferences.cpp:39
size_t strlen(const char *s)
Definition: strlen.c:3
constexpr PrintFloat::Mode DecimalDisplay
void assert_expression_prints_to(Expression *e, const char *result, PrintFloat::Mode mode=ScientificDisplay, Expression::ComplexFormat format=Cartesian, int bufferSize=250)
#define INFINITY
Definition: math.h:29
void quiz_print(const char *message)
Definition: runner.cpp:7
int strcmp(const char *s1, const char *s2)
Definition: strcmp.c:3
constexpr Expression::ComplexFormat Polar
void setDisplayMode(PrintFloat::Mode mode)
Definition: preferences.cpp:31
virtual int writeTextInBuffer(char *buffer, int bufferSize, int numberOfSignificantDigits=PrintFloat::k_numberOfStoredSignificantDigits) const =0