Skip to content

Commit 330886f

Browse files
lodagrowesm
authored andcommitted
TST: Add tests for core.common.EngFormatter.
1 parent 188d828 commit 330886f

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

pandas/tests/test_common.py

+162
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,168 @@ def test_2d_float32(self):
267267
expected[:, [2, 4]] = np.nan
268268
tm.assert_almost_equal(result, expected)
269269

270+
class TestEngFormatter(unittest.TestCase):
271+
def compare(self, formatter, input, output):
272+
formatted_input = formatter(input)
273+
msg = "formatting of %s results in '%s', expected '%s'" % (str(input),
274+
formatted_input, output)
275+
self.assertEqual(formatted_input, output, msg)
276+
277+
def compare_all(self, formatter, in_out):
278+
"""
279+
Parameters:
280+
-----------
281+
formatter: EngFormatter under test
282+
in_out: list of tuples. Each tuple = (number, expected_formatting)
283+
284+
It is tested if 'formatter(number) == expected_formatting'.
285+
*number* should be >= 0 because formatter(-number) == fmt is also
286+
tested. *fmt* is derived from *expected_formatting*
287+
"""
288+
for input, output in in_out:
289+
self.compare(formatter, input, output)
290+
self.compare(formatter, -input, "-" + output[1:])
291+
292+
def test_exponents_with_eng_prefix(self):
293+
formatter = common.EngFormatter(accuracy=3, use_eng_prefix=True)
294+
f = np.sqrt(2)
295+
in_out = [(f * 10 ** -24, " 1.414y"),
296+
(f * 10 ** -23, " 14.142y"),
297+
(f * 10 ** -22, " 141.421y"),
298+
(f * 10 ** -21, " 1.414z"),
299+
(f * 10 ** -20, " 14.142z"),
300+
(f * 10 ** -19, " 141.421z"),
301+
(f * 10 ** -18, " 1.414a"),
302+
(f * 10 ** -17, " 14.142a"),
303+
(f * 10 ** -16, " 141.421a"),
304+
(f * 10 ** -15, " 1.414f"),
305+
(f * 10 ** -14, " 14.142f"),
306+
(f * 10 ** -13, " 141.421f"),
307+
(f * 10 ** -12, " 1.414p"),
308+
(f * 10 ** -11, " 14.142p"),
309+
(f * 10 ** -10, " 141.421p"),
310+
(f * 10 ** -9, " 1.414n"),
311+
(f * 10 ** -8, " 14.142n"),
312+
(f * 10 ** -7, " 141.421n"),
313+
(f * 10 ** -6, " 1.414u"),
314+
(f * 10 ** -5, " 14.142u"),
315+
(f * 10 ** -4, " 141.421u"),
316+
(f * 10 ** -3, " 1.414m"),
317+
(f * 10 ** -2, " 14.142m"),
318+
(f * 10 ** -1, " 141.421m"),
319+
(f * 10 ** 0, " 1.414"),
320+
(f * 10 ** 1, " 14.142"),
321+
(f * 10 ** 2, " 141.421"),
322+
(f * 10 ** 3, " 1.414k"),
323+
(f * 10 ** 4, " 14.142k"),
324+
(f * 10 ** 5, " 141.421k"),
325+
(f * 10 ** 6, " 1.414M"),
326+
(f * 10 ** 7, " 14.142M"),
327+
(f * 10 ** 8, " 141.421M"),
328+
(f * 10 ** 9, " 1.414G"),
329+
(f * 10 ** 10, " 14.142G"),
330+
(f * 10 ** 11, " 141.421G"),
331+
(f * 10 ** 12, " 1.414T"),
332+
(f * 10 ** 13, " 14.142T"),
333+
(f * 10 ** 14, " 141.421T"),
334+
(f * 10 ** 15, " 1.414P"),
335+
(f * 10 ** 16, " 14.142P"),
336+
(f * 10 ** 17, " 141.421P"),
337+
(f * 10 ** 18, " 1.414E"),
338+
(f * 10 ** 19, " 14.142E"),
339+
(f * 10 ** 20, " 141.421E"),
340+
(f * 10 ** 21, " 1.414Z"),
341+
(f * 10 ** 22, " 14.142Z"),
342+
(f * 10 ** 23, " 141.421Z"),
343+
(f * 10 ** 24, " 1.414Y"),
344+
(f * 10 ** 25, " 14.142Y"),
345+
(f * 10 ** 26, " 141.421Y")]
346+
self.compare_all(formatter, in_out)
347+
348+
def test_exponents_without_eng_prefix(self):
349+
formatter = common.EngFormatter(accuracy=4, use_eng_prefix=False)
350+
f = np.pi
351+
in_out = [(f * 10 ** -24, " 3.1416E-24"),
352+
(f * 10 ** -23, " 31.4159E-24"),
353+
(f * 10 ** -22, " 314.1593E-24"),
354+
(f * 10 ** -21, " 3.1416E-21"),
355+
(f * 10 ** -20, " 31.4159E-21"),
356+
(f * 10 ** -19, " 314.1593E-21"),
357+
(f * 10 ** -18, " 3.1416E-18"),
358+
(f * 10 ** -17, " 31.4159E-18"),
359+
(f * 10 ** -16, " 314.1593E-18"),
360+
(f * 10 ** -15, " 3.1416E-15"),
361+
(f * 10 ** -14, " 31.4159E-15"),
362+
(f * 10 ** -13, " 314.1593E-15"),
363+
(f * 10 ** -12, " 3.1416E-12"),
364+
(f * 10 ** -11, " 31.4159E-12"),
365+
(f * 10 ** -10, " 314.1593E-12"),
366+
(f * 10 ** -9, " 3.1416E-09"),
367+
(f * 10 ** -8, " 31.4159E-09"),
368+
(f * 10 ** -7, " 314.1593E-09"),
369+
(f * 10 ** -6, " 3.1416E-06"),
370+
(f * 10 ** -5, " 31.4159E-06"),
371+
(f * 10 ** -4, " 314.1593E-06"),
372+
(f * 10 ** -3, " 3.1416E-03"),
373+
(f * 10 ** -2, " 31.4159E-03"),
374+
(f * 10 ** -1, " 314.1593E-03"),
375+
(f * 10 ** 0, " 3.1416E+00"),
376+
(f * 10 ** 1, " 31.4159E+00"),
377+
(f * 10 ** 2, " 314.1593E+00"),
378+
(f * 10 ** 3, " 3.1416E+03"),
379+
(f * 10 ** 4, " 31.4159E+03"),
380+
(f * 10 ** 5, " 314.1593E+03"),
381+
(f * 10 ** 6, " 3.1416E+06"),
382+
(f * 10 ** 7, " 31.4159E+06"),
383+
(f * 10 ** 8, " 314.1593E+06"),
384+
(f * 10 ** 9, " 3.1416E+09"),
385+
(f * 10 ** 10, " 31.4159E+09"),
386+
(f * 10 ** 11, " 314.1593E+09"),
387+
(f * 10 ** 12, " 3.1416E+12"),
388+
(f * 10 ** 13, " 31.4159E+12"),
389+
(f * 10 ** 14, " 314.1593E+12"),
390+
(f * 10 ** 15, " 3.1416E+15"),
391+
(f * 10 ** 16, " 31.4159E+15"),
392+
(f * 10 ** 17, " 314.1593E+15"),
393+
(f * 10 ** 18, " 3.1416E+18"),
394+
(f * 10 ** 19, " 31.4159E+18"),
395+
(f * 10 ** 20, " 314.1593E+18"),
396+
(f * 10 ** 21, " 3.1416E+21"),
397+
(f * 10 ** 22, " 31.4159E+21"),
398+
(f * 10 ** 23, " 314.1593E+21"),
399+
(f * 10 ** 24, " 3.1416E+24"),
400+
(f * 10 ** 25, " 31.4159E+24"),
401+
(f * 10 ** 26, " 314.1593E+24")]
402+
self.compare_all(formatter, in_out)
403+
404+
def test_rounding(self):
405+
formatter = common.EngFormatter(accuracy=3, use_eng_prefix=True)
406+
in_out = [(5.55555, ' 5.556'),
407+
(55.5555, ' 55.556'),
408+
(555.555, ' 555.555'),
409+
(5555.55, ' 5.556k'),
410+
(55555.5, ' 55.556k'),
411+
(555555, ' 555.555k')]
412+
self.compare_all(formatter, in_out)
413+
414+
formatter = common.EngFormatter(accuracy=1, use_eng_prefix=True)
415+
in_out = [(5.55555, ' 5.6'),
416+
(55.5555, ' 55.6'),
417+
(555.555, ' 555.6'),
418+
(5555.55, ' 5.6k'),
419+
(55555.5, ' 55.6k'),
420+
(555555, ' 555.6k')]
421+
self.compare_all(formatter, in_out)
422+
423+
formatter = common.EngFormatter(accuracy=0, use_eng_prefix=True)
424+
in_out = [(5.55555, ' 6'),
425+
(55.5555, ' 56'),
426+
(555.555, ' 556'),
427+
(5555.55, ' 6k'),
428+
(55555.5, ' 56k'),
429+
(555555, ' 556k')]
430+
self.compare_all(formatter, in_out)
431+
270432
if __name__ == '__main__':
271433
import nose
272434
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)