Parser of berki style problems and generator of latex file
Samo Penic
2018-11-24 8fe4d9b44bde3cec33314bcafe2130b6e4dac7af
commit | author | age
404823 1 import unittest
c9c8d5 2 from aoi_gen import Variable
404823 3
SP 4
5 class FormatTest(unittest.TestCase):
6     def test_Exceptions(self):
7         exception_cases = [None, "hello"]
8         exception_results = [ValueError, ValueError]
9
6c2245 10         sci2 = Variable.FormatterFactory().get_formatter("sci 2")
404823 11         for case, result in zip(exception_cases, exception_results):
SP 12             with self.assertRaises(result):
13                 sci2.toFormat(case)
14
15     def test_inputStrings(self):
16         # test eng formatter
17         testcases = [
18             1.235678910111213,
19             1.2,
20             3,
21             "3.5",
22             "0.05",
23             -1.2343567,
24             -3,
25             1.123e-6,
26             -1.123e6,
27             9.8123e255,
28             0,
29         ]
30         sci2_solutions = [
8fe4d9 31             "1,\!2",
SP 32             "1,\!2",
33             "3,\!0",
34             "3,\!5",
35             "5,\!0 \cdot 10^{-2}",
36             "-1,\!2",
37             "-3,\!0",
38             "1,\!1 \cdot 10^{-6}",
39             "-1,\!1 \cdot 10^{6}",
40             "9,\!8 \cdot 10^{255}",
41             "0,\!0",
404823 42         ]
SP 43
5288d6 44         sci3_solutions = [
8fe4d9 45             "1,\!24",
SP 46             "1,\!20",
47             "3,\!00",
48             "3,\!50",
49             "5,\!00 \cdot 10^{-2}",
50             "-1,\!23",
51             "-3,\!00",
52             "1,\!12 \cdot 10^{-6}",
53             "-1,\!12 \cdot 10^{6}",
54             "9,\!81 \cdot 10^{255}",
55             "0,\!00",
5288d6 56         ]
8fe4d9 57
SP 58
6c2245 59         sci2 = Variable.FormatterFactory().get_formatter("sci 2")
33c054 60         for case, result in zip(testcases, sci2_solutions):
5288d6 61             self.assertEqual(result, sci2.toFormat(case))
SP 62
6c2245 63         sci3 = Variable.FormatterFactory().get_formatter("sci 3")
33c054 64         for case, result in zip(testcases, sci3_solutions):
5288d6 65             self.assertEqual(result, sci3.toFormat(case))
8fe4d9 66
SP 67
68
69     def test_additionalDecTests(self):
70         testcases = [
71             1.235678910111213,
72             1.2,
73             3,
74             "3.5",
75             "0.05",
76             -1.2343567,
77             -3,
78             1.123e-6,
79             -1.123e6,
80             0,
81             32,
82         ]
83         dec2_solutions = [
84             "1,\!2",
85             "1,\!2",
86             "3,\!0",
87             "3,\!5",
88             "0,\!050",
89             "-1,\!2",
90             "-3,\!0",
91             "0,\!0000011",
92             "-1100000",
93             "0,\!0",
94             "32"
95         ]
96         dec1_solutions=[
97             "1",
98             "1",
99             "3",
100             "4",
101             "0,\!05",
102             "-1",
103             "-3",
104             "0,\!000001",
105             "-1000000",
106             "0",
107             "30",
108         ]
109
110         dec2 = Variable.FormatterFactory().get_formatter("dec 2")
111         for case, result in zip(testcases, dec2_solutions):
112             self.assertEqual(result, dec2.toFormat(case))
113
114         dec1 = Variable.FormatterFactory().get_formatter("dec 1")
115         for case, result in zip(testcases, dec1_solutions):
116             self.assertEqual(result, dec1.toFormat(case))
7b5760 117
SP 118     def test_valueClass(self):
119         testcases = [
120             1.235678910111213,
121             1.2,
122             3,
123             "3.5",
124             "0.05",
125             -1.2343567,
126             -3,
127             1.123e-6,
128             -1.123e6,
129             9.8123e255,
130             0,
131         ]
132         sci2_solutions = [
8fe4d9 133             "$1,\!2\,$",
SP 134             "$1,\!2\,$",
135             "$3,\!0\,$",
136             "$3,\!5\,$",
137             "$5,\!0 \cdot 10^{-2}\,$",
138             "$-1,\!2\,$",
139             "$-3,\!0\,$",
140             "$1,\!1 \cdot 10^{-6}\,$",
141             "$-1,\!1 \cdot 10^{6}\,$",
142             "$9,\!8 \cdot 10^{255}\,$",
143             "$0,\!0\,$",
7b5760 144         ]
SP 145
146         sci3_solutions = [
8fe4d9 147             "$1,\!24\,$",
SP 148             "$1,\!20\,$",
149             "$3,\!00\,$",
150             "$3,\!50\,$",
151             "$5,\!00 \cdot 10^{-2}\,$",
152             "$-1,\!23\,$",
153             "$-3,\!00\,$",
154             "$1,\!12 \cdot 10^{-6}\,$",
155             "$-1,\!12 \cdot 10^{6}\,$",
156             "$9,\!81 \cdot 10^{255}\,$",
157             "$0,\!00\,$",
7b5760 158         ]
SP 159
78d798 160         sci2_rounded = [
ef19a9 161             1.2,
SP 162             1.2,
163             3.0,
164             3.5,
165             0.05,
166             -1.2,
167             -3.0,
168             1.1e-6,
169             -1.1e6,
170             9.8e255,
78d798 171             0,
ef19a9 172         ]
33c054 173         for case, result in zip(testcases, sci2_solutions):
6c2245 174             self.assertEqual(result, Variable.Variable(case, "sci 2").format_as_tex())
7b5760 175
33c054 176         for case, result in zip(testcases, sci3_solutions):
6c2245 177             self.assertEqual(result, Variable.Variable(case, "sci 3").format_as_tex())
ef19a9 178
SP 179         for case, result in zip(testcases, sci2_rounded):
78d798 180             self.assertEqual(
SP 181                 result, Variable.Variable(case, "sci 2").get_formatted_value()
182             )