| 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 | testcases = [ | |
| 17 | 1.235678910111213, | |
| 18 | 1.2, | |
| 19 | 3, | |
| 20 | "3.5", | |
| 21 | "0.05", | |
| 22 | -1.2343567, | |
| 23 | -3, | |
| 24 | 1.123e-6, | |
| 25 | -1.123e6, | |
| 26 | 9.8123e255, | |
| 27 | 0, | |
| 32d3f2 | 28 | 45, |
| SP | 29 | |
| 404823 | 30 | ] |
| SP | 31 | sci2_solutions = [ |
| 8fe4d9 | 32 | "1,\!2", |
| SP | 33 | "1,\!2", |
| 34 | "3,\!0", | |
| 35 | "3,\!5", | |
| 36 | "5,\!0 \cdot 10^{-2}", | |
| 37 | "-1,\!2", | |
| 38 | "-3,\!0", | |
| 39 | "1,\!1 \cdot 10^{-6}", | |
| 40 | "-1,\!1 \cdot 10^{6}", | |
| 41 | "9,\!8 \cdot 10^{255}", | |
| 42 | "0,\!0", | |
| 32d3f2 | 43 | "4,\!5 \cdot 10^{1}" |
| 404823 | 44 | ] |
| SP | 45 | |
| 5288d6 | 46 | sci3_solutions = [ |
| 8fe4d9 | 47 | "1,\!24", |
| SP | 48 | "1,\!20", |
| 49 | "3,\!00", | |
| 50 | "3,\!50", | |
| 51 | "5,\!00 \cdot 10^{-2}", | |
| 52 | "-1,\!23", | |
| 53 | "-3,\!00", | |
| 54 | "1,\!12 \cdot 10^{-6}", | |
| 55 | "-1,\!12 \cdot 10^{6}", | |
| 56 | "9,\!81 \cdot 10^{255}", | |
| 57 | "0,\!00", | |
| 32d3f2 | 58 | "4,\!50 \cdot 10^{1}" |
| 5288d6 | 59 | ] |
| 8fe4d9 | 60 | |
| 32d3f2 | 61 | eng2_solutions = [ |
| SP | 62 | "1,\!2", |
| 63 | "1,\!2", | |
| 64 | "3,\!0", | |
| 65 | "3,\!5", | |
| 66 | "50 \cdot 10^{-3}", | |
| 67 | "-1,\!2", | |
| 68 | "-3,\!0", | |
| 69 | "1,\!1 \cdot 10^{-6}", | |
| 70 | "-1,\!1 \cdot 10^{6}", | |
| 71 | "9,\!8 \cdot 10^{255}", | |
| 72 | "0,\!0", | |
| 73 | "45" | |
| 74 | ] | |
| 75 | ||
| 76 | eng1_solutions = [ | |
| 77 | "1", | |
| 78 | "1", | |
| 79 | "3", | |
| 80 | "4", | |
| 81 | "50 \cdot 10^{-3}", | |
| 82 | "-1", | |
| 83 | "-3", | |
| 84 | "1 \cdot 10^{-6}", | |
| 85 | "-1 \cdot 10^{6}", | |
| 86 | "10 \cdot 10^{255}", | |
| 87 | "0", | |
| 88 | "40" | |
| 89 | ] | |
| 8fe4d9 | 90 | |
| 6c2245 | 91 | sci2 = Variable.FormatterFactory().get_formatter("sci 2") |
| 33c054 | 92 | for case, result in zip(testcases, sci2_solutions): |
| 5288d6 | 93 | self.assertEqual(result, sci2.toFormat(case)) |
| SP | 94 | |
| 6c2245 | 95 | sci3 = Variable.FormatterFactory().get_formatter("sci 3") |
| 33c054 | 96 | for case, result in zip(testcases, sci3_solutions): |
| 5288d6 | 97 | self.assertEqual(result, sci3.toFormat(case)) |
| 8fe4d9 | 98 | |
| 32d3f2 | 99 | eng1 = Variable.FormatterFactory().get_formatter("eng 1") |
| SP | 100 | for case, result in zip(testcases, eng1_solutions): |
| 101 | self.assertEqual(result, eng1.toFormat(case)) | |
| 102 | ||
| 103 | eng2 = Variable.FormatterFactory().get_formatter("eng 2") | |
| 104 | for case, result in zip(testcases, eng2_solutions): | |
| 105 | self.assertEqual(result, eng2.toFormat(case)) | |
| 8fe4d9 | 106 | |
| SP | 107 | |
| 108 | def test_additionalDecTests(self): | |
| 109 | testcases = [ | |
| 110 | 1.235678910111213, | |
| 111 | 1.2, | |
| 112 | 3, | |
| 113 | "3.5", | |
| 114 | "0.05", | |
| 115 | -1.2343567, | |
| 116 | -3, | |
| 117 | 1.123e-6, | |
| 118 | -1.123e6, | |
| 119 | 0, | |
| 120 | 32, | |
| 32d3f2 | 121 | ] |
| SP | 122 | |
| 123 | prefix2_solutions =[ | |
| 124 | "1,\!2\,", | |
| 125 | "1,\!2\,", | |
| 126 | "3,\!0\,", | |
| 127 | "3,\!5\,", | |
| 128 | "50\,\mathrm{m}", | |
| 129 | "-1,\!2\,", | |
| 130 | "-3,\!0\,", | |
| 131 | "1,\!1\,\mathrm{\\upmu}", | |
| 132 | "-1,\!1\,\mathrm{M}", | |
| 133 | "0,\!0\,", | |
| 134 | "32\," | |
| 135 | ] | |
| 136 | prefix1_solutions = [ | |
| 137 | "1\,", | |
| 138 | "1\,", | |
| 139 | "3\,", | |
| 140 | "4\,", | |
| 141 | "50\,\mathrm{m}", | |
| 142 | "-1\,", | |
| 143 | "-3\,", | |
| 144 | "1\,\mathrm{\\upmu}", | |
| 145 | "-1\,\mathrm{M}", | |
| 146 | "0\,", | |
| 147 | "30\," | |
| 8fe4d9 | 148 | ] |
| SP | 149 | dec2_solutions = [ |
| 150 | "1,\!2", | |
| 151 | "1,\!2", | |
| 152 | "3,\!0", | |
| 153 | "3,\!5", | |
| 154 | "0,\!050", | |
| 155 | "-1,\!2", | |
| 156 | "-3,\!0", | |
| 157 | "0,\!0000011", | |
| 158 | "-1100000", | |
| 159 | "0,\!0", | |
| 160 | "32" | |
| 161 | ] | |
| 162 | dec1_solutions=[ | |
| 163 | "1", | |
| 164 | "1", | |
| 165 | "3", | |
| 166 | "4", | |
| 167 | "0,\!05", | |
| 168 | "-1", | |
| 169 | "-3", | |
| 170 | "0,\!000001", | |
| 171 | "-1000000", | |
| 172 | "0", | |
| 173 | "30", | |
| 174 | ] | |
| 175 | ||
| 176 | dec2 = Variable.FormatterFactory().get_formatter("dec 2") | |
| 177 | for case, result in zip(testcases, dec2_solutions): | |
| 178 | self.assertEqual(result, dec2.toFormat(case)) | |
| 179 | ||
| 180 | dec1 = Variable.FormatterFactory().get_formatter("dec 1") | |
| 181 | for case, result in zip(testcases, dec1_solutions): | |
| 182 | self.assertEqual(result, dec1.toFormat(case)) | |
| 7b5760 | 183 | |
| 32d3f2 | 184 | prefix2 = Variable.FormatterFactory().get_formatter("prefix 2") |
| SP | 185 | for case, result in zip(testcases, prefix2_solutions): |
| 186 | self.assertEqual(result, prefix2.toFormat(case)) | |
| 187 | ||
| 188 | prefix1 = Variable.FormatterFactory().get_formatter("prefix 1") | |
| 189 | for case, result in zip(testcases, prefix1_solutions): | |
| 190 | self.assertEqual(result, prefix1.toFormat(case)) | |
| 191 | ||
| 7b5760 | 192 | def test_valueClass(self): |
| SP | 193 | testcases = [ |
| 194 | 1.235678910111213, | |
| 195 | 1.2, | |
| 196 | 3, | |
| 197 | "3.5", | |
| 198 | "0.05", | |
| 199 | -1.2343567, | |
| 200 | -3, | |
| 201 | 1.123e-6, | |
| 202 | -1.123e6, | |
| 203 | 9.8123e255, | |
| 204 | 0, | |
| 32d3f2 | 205 | 45, |
| 7b5760 | 206 | ] |
| SP | 207 | sci2_solutions = [ |
| 8fe4d9 | 208 | "$1,\!2\,$", |
| SP | 209 | "$1,\!2\,$", |
| 210 | "$3,\!0\,$", | |
| 211 | "$3,\!5\,$", | |
| 212 | "$5,\!0 \cdot 10^{-2}\,$", | |
| 213 | "$-1,\!2\,$", | |
| 214 | "$-3,\!0\,$", | |
| 215 | "$1,\!1 \cdot 10^{-6}\,$", | |
| 216 | "$-1,\!1 \cdot 10^{6}\,$", | |
| 217 | "$9,\!8 \cdot 10^{255}\,$", | |
| 218 | "$0,\!0\,$", | |
| 32d3f2 | 219 | "$4,\!5 \cdot 10^{1}\,$", |
| 7b5760 | 220 | ] |
| SP | 221 | |
| 222 | sci3_solutions = [ | |
| 8fe4d9 | 223 | "$1,\!24\,$", |
| SP | 224 | "$1,\!20\,$", |
| 225 | "$3,\!00\,$", | |
| 226 | "$3,\!50\,$", | |
| 227 | "$5,\!00 \cdot 10^{-2}\,$", | |
| 228 | "$-1,\!23\,$", | |
| 229 | "$-3,\!00\,$", | |
| 230 | "$1,\!12 \cdot 10^{-6}\,$", | |
| 231 | "$-1,\!12 \cdot 10^{6}\,$", | |
| 232 | "$9,\!81 \cdot 10^{255}\,$", | |
| 233 | "$0,\!00\,$", | |
| 32d3f2 | 234 | "$4,\!50 \cdot 10^{1}\,$", |
| 7b5760 | 235 | ] |
| SP | 236 | |
| 78d798 | 237 | sci2_rounded = [ |
| ef19a9 | 238 | 1.2, |
| SP | 239 | 1.2, |
| 240 | 3.0, | |
| 241 | 3.5, | |
| 242 | 0.05, | |
| 243 | -1.2, | |
| 244 | -3.0, | |
| 245 | 1.1e-6, | |
| 246 | -1.1e6, | |
| 247 | 9.8e255, | |
| 78d798 | 248 | 0, |
| 32d3f2 | 249 | 45 |
| ef19a9 | 250 | ] |
| 32d3f2 | 251 | |
| SP | 252 | sci1_rounded = [ |
| 253 | 1, | |
| 254 | 1, | |
| 255 | 3, | |
| 256 | 4, | |
| 257 | 50e-3, | |
| 258 | -1, | |
| 259 | -3, | |
| 260 | 1e-6, | |
| 261 | -1e6, | |
| 262 | 10e255, | |
| 263 | 0, | |
| 264 | 40 | |
| 265 | ] | |
| 266 | eng1_rounded=sci1_rounded | |
| 267 | eng2_rounded=sci2_rounded | |
| 268 | ||
| 33c054 | 269 | for case, result in zip(testcases, sci2_solutions): |
| 6c2245 | 270 | self.assertEqual(result, Variable.Variable(case, "sci 2").format_as_tex()) |
| 7b5760 | 271 | |
| 33c054 | 272 | for case, result in zip(testcases, sci3_solutions): |
| 6c2245 | 273 | self.assertEqual(result, Variable.Variable(case, "sci 3").format_as_tex()) |
| ef19a9 | 274 | |
| SP | 275 | for case, result in zip(testcases, sci2_rounded): |
| 78d798 | 276 | self.assertEqual( |
| SP | 277 | result, Variable.Variable(case, "sci 2").get_formatted_value() |
| 278 | ) | |
| 32d3f2 | 279 | for case, result in zip(testcases, sci1_rounded): |
| SP | 280 | self.assertEqual( |
| 281 | result, Variable.Variable(case, "sci 1").get_formatted_value() | |
| 282 | ) | |
| 283 | for case, result in zip(testcases, eng1_rounded): | |
| 284 | self.assertEqual( | |
| 285 | result, Variable.Variable(case, "eng 1").get_formatted_value() | |
| 286 | ) | |
| 287 | for case, result in zip(testcases, eng2_rounded): | |
| 288 | self.assertEqual( | |
| 289 | result, Variable.Variable(case, "eng 2").get_formatted_value() | |
| 290 | ) | |
| 291 | ||