You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

85 lines
2.7 KiB

  1. import nilmdb
  2. from nilmdb.utils.printf import *
  3. from nose.tools import *
  4. from nose.tools import assert_raises
  5. import os
  6. import sys
  7. import cStringIO
  8. from testutil.helpers import *
  9. from nilmdb.utils import timestamper
  10. class TestTimestamper(object):
  11. # Not a very comprehensive test, but it's good enough.
  12. def test_timestamper(self):
  13. def join(list):
  14. return "\n".join(list) + "\n"
  15. start = nilmdb.utils.time.parse_time("03/24/2012")
  16. lines_in = [ "hello", "world", "hello world", "# commented out" ]
  17. lines_out = [ "1332561600.000000 hello",
  18. "1332561600.000125 world",
  19. "1332561600.000250 hello world" ]
  20. # full
  21. input = cStringIO.StringIO(join(lines_in))
  22. ts = timestamper.TimestamperRate(input, start, 8000)
  23. foo = ts.readlines()
  24. eq_(foo, join(lines_out))
  25. in_("TimestamperRate(..., start=", str(ts))
  26. # first 30 or so bytes means the first 2 lines
  27. input = cStringIO.StringIO(join(lines_in))
  28. ts = timestamper.TimestamperRate(input, start, 8000)
  29. foo = ts.readlines(30)
  30. eq_(foo, join(lines_out[0:2]))
  31. # stop iteration early
  32. input = cStringIO.StringIO(join(lines_in))
  33. ts = timestamper.TimestamperRate(input, start, 8000,
  34. 1332561600.000200)
  35. foo = ""
  36. for line in ts:
  37. foo += line
  38. eq_(foo, join(lines_out[0:2]))
  39. # stop iteration early (readlines)
  40. input = cStringIO.StringIO(join(lines_in))
  41. ts = timestamper.TimestamperRate(input, start, 8000,
  42. 1332561600.000200)
  43. foo = ts.readlines()
  44. eq_(foo, join(lines_out[0:2]))
  45. # stop iteration really early
  46. input = cStringIO.StringIO(join(lines_in))
  47. ts = timestamper.TimestamperRate(input, start, 8000,
  48. 1332561600.000000)
  49. foo = ts.readlines()
  50. eq_(foo, "")
  51. # use iterator
  52. input = cStringIO.StringIO(join(lines_in))
  53. ts = timestamper.TimestamperRate(input, start, 8000)
  54. foo = ""
  55. for line in ts:
  56. foo += line
  57. eq_(foo, join(lines_out))
  58. # check that TimestamperNow gives similar result
  59. input = cStringIO.StringIO(join(lines_in))
  60. ts = timestamper.TimestamperNow(input)
  61. foo = ts.readlines()
  62. ne_(foo, join(lines_out))
  63. eq_(len(foo), len(join(lines_out)))
  64. eq_(str(ts), "TimestamperNow(...)")
  65. # Test passing a file (should be empty)
  66. ts = timestamper.TimestamperNow("/dev/null")
  67. for line in ts:
  68. raise AssertionError
  69. ts.close()