PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/test_apiary/timestamp.py

https://bitbucket.org/lindenlab/apiary/
Python | 202 lines | 131 code | 47 blank | 24 comment | 1 complexity | 807afea25bdfb69b6429a65130d1ba18 MD5 | raw file
  1. #
  2. # $LicenseInfo:firstyear=2010&license=mit$
  3. #
  4. # Copyright (c) 2010, Linden Research, Inc.
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. # $/LicenseInfo$
  24. #
  25. import unittest
  26. from apiary.tools.timestamp import TimeStamp
  27. class TestConstruction(unittest.TestCase):
  28. def testFloatConstruction(self):
  29. ts = TimeStamp(12.3456789)
  30. self.assertEqual(ts.seconds, 12)
  31. self.assertEqual(ts.micros, 345679)
  32. ts = TimeStamp(123.456789)
  33. self.assertEqual(ts.seconds, 123)
  34. self.assertEqual(ts.micros, 456789)
  35. ts = TimeStamp(1234.56789)
  36. self.assertEqual(ts.seconds, 1234)
  37. self.assertEqual(ts.micros, 567890)
  38. ts = TimeStamp(12345.6789)
  39. self.assertEqual(ts.seconds, 12345)
  40. self.assertEqual(ts.micros, 678900)
  41. ts = TimeStamp(123456.789)
  42. self.assertEqual(ts.seconds, 123456)
  43. self.assertEqual(ts.micros, 789000)
  44. ts = TimeStamp(1234567.89)
  45. self.assertEqual(ts.seconds, 1234567)
  46. self.assertEqual(ts.micros, 890000)
  47. ts = TimeStamp(12345678.9)
  48. self.assertEqual(ts.seconds, 12345678)
  49. self.assertEqual(ts.micros, 900000)
  50. ts = TimeStamp(123456789.)
  51. self.assertEqual(ts.seconds, 123456789)
  52. self.assertEqual(ts.micros, 0)
  53. def testIntConstruction(self):
  54. ts = TimeStamp(123456789)
  55. self.assertEqual(ts.seconds, 123456789)
  56. self.assertEqual(ts.micros, 0)
  57. ts = TimeStamp(123, 456789)
  58. self.assertEqual(ts.seconds, 123)
  59. self.assertEqual(ts.micros, 456789)
  60. def testStringConstruction(self):
  61. ts = TimeStamp('0.123456')
  62. self.assertEqual(ts.seconds, 0)
  63. self.assertEqual(ts.micros, 123456)
  64. ts = TimeStamp('12.3456789')
  65. self.assertEqual(ts.seconds, 12)
  66. self.assertEqual(ts.micros, 345679)
  67. ts = TimeStamp('123.456789')
  68. self.assertEqual(ts.seconds, 123)
  69. self.assertEqual(ts.micros, 456789)
  70. ts = TimeStamp('1234.56789')
  71. self.assertEqual(ts.seconds, 1234)
  72. self.assertEqual(ts.micros, 567890)
  73. ts = TimeStamp('12345.6789')
  74. self.assertEqual(ts.seconds, 12345)
  75. self.assertEqual(ts.micros, 678900)
  76. ts = TimeStamp('123456.789')
  77. self.assertEqual(ts.seconds, 123456)
  78. self.assertEqual(ts.micros, 789000)
  79. ts = TimeStamp('1234567.89')
  80. self.assertEqual(ts.seconds, 1234567)
  81. self.assertEqual(ts.micros, 890000)
  82. ts = TimeStamp('12345678.9')
  83. self.assertEqual(ts.seconds, 12345678)
  84. self.assertEqual(ts.micros, 900000)
  85. ts = TimeStamp('123456789.')
  86. self.assertEqual(ts.seconds, 123456789)
  87. self.assertEqual(ts.micros, 0)
  88. class TestOperations(unittest.TestCase):
  89. def testEquality(self):
  90. t1 = TimeStamp(100, 900000)
  91. t1x = TimeStamp(100.9)
  92. self.assert_(t1 == t1x)
  93. def testOrdering(self):
  94. t1 = TimeStamp(100, 900000)
  95. t2 = TimeStamp(100, 900001)
  96. t3 = TimeStamp(101, 000002)
  97. self.assert_(t1 < t2)
  98. self.assert_(t2 < t3)
  99. self.assert_(t1 < t3)
  100. self.assert_(t3 > t1)
  101. def testDifference(self):
  102. t1 = TimeStamp(100, 900000)
  103. t2 = TimeStamp(100, 900001)
  104. t3 = TimeStamp(101, 000002)
  105. t1x = TimeStamp(100.9)
  106. self.assertEqual(t1 - t1x, TimeStamp(0,0))
  107. self.assertEqual(t2 - t1, TimeStamp(0,1))
  108. self.assertEqual(t3 - t2, TimeStamp(0,100001))
  109. self.assertEqual(t3 - t1, TimeStamp(0,100002))
  110. self.assertEqual(t1 - t2, TimeStamp(-1,999999))
  111. self.assertEqual(t2 - t3, TimeStamp(-1,899999))
  112. self.assertEqual(t1 - t3, TimeStamp(-1,899998))
  113. def testAdding(self):
  114. t1 = TimeStamp(100, 900000)
  115. t2 = TimeStamp(100, 900001)
  116. t3 = TimeStamp(101, 0)
  117. t4 = TimeStamp(101, 000002)
  118. d0 = TimeStamp(0,0)
  119. d21 = TimeStamp(0,1)
  120. d31 = TimeStamp(0,100000)
  121. d41 = TimeStamp(0,100002)
  122. d42 = TimeStamp(0,100001)
  123. self.assertEqual(t1 + d0, t1)
  124. self.assertEqual(t1 + d21, t2)
  125. self.assertEqual(t1 + d31, t3)
  126. self.assertEqual(t1 + d41, t4)
  127. self.assertEqual(t2 + d42, t4)
  128. def testAddInPlace(self):
  129. t1 = TimeStamp(100, 900000)
  130. t1 += TimeStamp(1,1)
  131. self.assertEqual(t1, TimeStamp(101, 900001))
  132. class TestStringConversion(unittest.TestCase):
  133. def testStr(self):
  134. self.assertEqual(str(TimeStamp(100,0)), '100.000000')
  135. self.assertEqual(str(TimeStamp(100,1)), '100.000001')
  136. self.assertEqual(str(TimeStamp(0)), '0.000000')
  137. def testRepr(self):
  138. self.assertEqual(repr(TimeStamp(100,0)), 'TimeStamp(100,0)')
  139. self.assertEqual(repr(TimeStamp(100,1)), 'TimeStamp(100,1)')
  140. self.assertEqual(repr(TimeStamp(0)), 'TimeStamp(0,0)')
  141. def testFloat(self):
  142. self.assertEqual(float(TimeStamp(100,0)), 100.0)
  143. self.assertEqual(float(TimeStamp(100,1)), 100.000001)
  144. self.assertEqual(float(TimeStamp(100,499999)), 100.499999)
  145. self.assertEqual(float(TimeStamp(100,500000)), 100.500000)
  146. self.assertEqual(float(TimeStamp(100,500001)), 100.500001)
  147. self.assertEqual(float(TimeStamp(100,999999)), 100.999999)
  148. self.assertEqual(float(TimeStamp(0)), 0.0)
  149. class TestRanges(unittest.TestCase):
  150. def testBig(self):
  151. ts = TimeStamp('1237237351.064861')
  152. self.assertEqual(ts.seconds, 1237237351)
  153. self.assertEqual(ts.micros, 64861)
  154. if __name__ == '__main__':
  155. unittest.main()