Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 65 additions & 66 deletions chaco/scales/tests/test_formatters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import print_function
import unittest

from chaco.scales.formatters import strftimeEx, TimeFormatter

Expand All @@ -7,80 +7,79 @@
# strftimeEx tests
#----------------------------------------------------------------

def test_strftimeEx_01():
t = 0.123
fmt = "%(ms)"
result = strftimeEx(fmt, t)
assert result == "123"
class TestStrftimeEx(unittest.TestCase):

def test_strftimeEx_02():
t = 0.123456
fmt = "%(us)"
result = strftimeEx(fmt, t)
assert result == "456"
def test_strftimeEx_01(self):
t = 0.123
fmt = "%(ms)"
result = strftimeEx(fmt, t)
self.assertEqual(result, "123")

def test_strftimeEx_03():
t = 0.678910
fmt = "%(ms)"
# According to the code, the number that replaces (ms) is *rounded*,
# so this formt should give "679".
result = strftimeEx(fmt, t)
assert result == "679"
def test_strftimeEx_02(self):
t = 0.123456
fmt = "%(us)"
result = strftimeEx(fmt, t)
self.assertEqual(result, "456")

def test_strftimeEx_04():
t = 0.678910
fmt = "%(ms).%(us)ms"
# According to the code, the number that replaces (ms) is *rounded*,
# so this formt should give "679.910ms". (See the next test case for the
# correct way to do this.)
result = strftimeEx(fmt, t)
expected = "679.910ms"
assert result == expected
def test_strftimeEx_03(self):
t = 0.678910
fmt = "%(ms)"
# According to the code, the number that replaces (ms) is *rounded*,
# so this formt should give "679".
result = strftimeEx(fmt, t)
self.assertEqual(result, "679")

def test_strftimeEx_04():
t = 0.678910
fmt = "%(ms_).%(us)ms"
# The format "%(ms_)" uses floor().
result = strftimeEx(fmt, t)
expected = "678.910ms"
print('result = "%s" expected = "%s"' % (result, expected))
assert result == expected
def test_strftimeEx_04(self):
t = 0.678910
fmt = "%(ms).%(us)ms"
# According to the code, the number that replaces (ms) is *rounded*,
# so this formt should give "679.910ms". (See the next test case for the
# correct way to do this.)
result = strftimeEx(fmt, t)
expected = "679.910ms"
self.assertEqual(result, expected)

def test_strftimeEx_05():
"""Test rounding that affects the seconds."""
t = 7.9999999
fmt = "%S %(ms_) %(us)"
result = strftimeEx(fmt, t)
expected = "08 000 000"
print('result = "%s" expected = "%s"' % (result, expected))
assert result == expected
def test_strftimeEx_04(self):
t = 0.678910
fmt = "%(ms_).%(us)ms"
# The format "%(ms_)" uses floor().
result = strftimeEx(fmt, t)
expected = "678.910ms"
self.assertEqual(result, expected)

def test_strftimeEx_06():
"""Test rounding that affects the seconds."""
t = 7.9996
fmt = "%S %(ms)"
result = strftimeEx(fmt, t)
expected = "08 000"
print('result = "%s" expected = "%s"' % (result, expected))
assert result == expected
def test_strftimeEx_05(self):
# Test rounding that affects the seconds.
t = 7.9999999
fmt = "%S %(ms_) %(us)"
result = strftimeEx(fmt, t)
expected = "08 000 000"
self.assertEqual(result, expected)

def test_strftimeEx_07():
"""Test rounding that affects the seconds."""
t = 7.9996
fmt = "%S %(ms_)"
result = strftimeEx(fmt, t)
expected = "07 999"
print('result = "%s" expected = "%s"' % (result, expected))
assert result == expected
def test_strftimeEx_06(self):
# Test rounding that affects the seconds.
t = 7.9996
fmt = "%S %(ms)"
result = strftimeEx(fmt, t)
expected = "08 000"
self.assertEqual(result, expected)

def test_strftimeEx_07(self):
# Test rounding that affects the seconds.
t = 7.9996
fmt = "%S %(ms_)"
result = strftimeEx(fmt, t)
expected = "07 999"
self.assertEqual(result, expected)

#----------------------------------------------------------------
# TimeFormatter tests
#----------------------------------------------------------------

def test_time_formatter_01():
tf = TimeFormatter()
ticks = [10.005, 10.0053, 10.0056]
labels = tf.format(ticks, char_width=130)
expected = ["5.000ms", "5.300ms", "5.600ms"]
print("labels =", labels, " expected =", expected)
assert labels == expected
class TestTimeFormatter(unittest.TestCase):

def test_time_formatter_01(self):
tf = TimeFormatter()
ticks = [10.005, 10.0053, 10.0056]
labels = tf.format(ticks, char_width=130)
expected = ["5.000ms", "5.300ms", "5.600ms"]
self.assertEqual(labels, expected)
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from __future__ import print_function

from traits.testing.unittest_tools import unittest

from numpy import array

from .formatters import BasicFormatter, OffsetFormatter
from .scales import Pow10Scale, FixedScale, LogScale, DefaultScale, ScaleSystem, frange
from ..formatters import BasicFormatter, OffsetFormatter
from ..scales import Pow10Scale, FixedScale, LogScale, DefaultScale, ScaleSystem, frange


class TicksTestCase(unittest.TestCase):
Expand Down Expand Up @@ -148,9 +146,7 @@ def test2_nice_sci(self):
val = lst[0]
for mdigits, desired in lst[1:]:
s = fmt._nice_sci(val, mdigits)
if s != desired:
print("Mismatch for", val, "; desired:", desired, "actual:", s)

self.assertEqual(s, desired)

def test_estimate_default_scale(self):
fmt = BasicFormatter()
Expand All @@ -177,12 +173,18 @@ def test_width_based_default_scale(self):
test_intervals = ((1, 100, 80),
(1, 100, 40),
(1, 100, 20),)
print()
for start, end, width in test_intervals:
res0 = [
(10.0, '10'), (20.0, '20'), (30.0, '30'), (40.0, '40'),
(50.0, '50'), (60.0, '60'), (70.0, '70'), (80.0, '80'),
(90.0, '90'), (100.0, '100')
]
res1 = [(25.0, '25'), (50.0, '50'), (75.0, '75'), (100.0, '100')]
res2 = [(100.0, '100')]
all_expected = [res0, res1, res2]

for (start, end, width), expected in zip(test_intervals, all_expected):
labels = scale.labels(start, end, char_width=width)
print("(%d,%d)" % (start,end), " avail:", width, end=" ")
print(" used:", sum([len(x[1]) for x in labels]))
return
self.assertEqual(labels, expected)

def test_scale_system(self):
scale = ScaleSystem(FixedScale(resolution = 1.0),
Expand All @@ -200,16 +202,14 @@ def test_scale_system(self):
(1, 10, 100),
(1, 10, 50),
(1, 10, 20),)
print()
for start, end, width in test_intervals:
expected_lengths = [40, 10, 5, 5, 1, 10, 10, 4]

for (start, end, width), ll in zip(test_intervals, expected_lengths):
labels = scale.labels(start, end, char_width=width)
print("(%d,%d)" % (start,end), " avail:", width, end=" ")
print(" used:", sum([len(x[1]) for x in labels]), end=" ")
print(list(zip(*labels))[1])
return
self.assertEqual(len(labels), ll)

class OffsetFormatterTestCase(TicksTestCase):

class OffsetFormatterTestCase(TicksTestCase):

def test_format(self):

Expand All @@ -226,14 +226,5 @@ def test_format(self):
scale = FixedScale(resolution = resol)
numlabels = 12
ticks = scale.ticks(start, end, numlabels)
print("range:", start, end)
labels = fmt.format(ticks, numlabels, None)
print("Labels:", labels)
print("estimated width:", fmt.estimate_width(start, end, numlabels))
print("actual width:", sum(map(len, labels)))



if __name__ == "__main__":
import nose
nose.run()
self.assertEqual(len(ticks), len(labels))
Loading