PageRenderTime 34ms CodeModel.GetById 11ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/historical/timestamp_test.py

https://bitbucket.org/lindenlab/apiary/
Python | 196 lines | 127 code | 45 blank | 24 comment | 1 complexity | ae7f6e1a4dfc72dff621b5475f473376 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
 26import unittest
 27
 28from timestamp import TimeStamp
 29
 30
 31class TestConstruction(unittest.TestCase):
 32
 33    def testFloatConstruction(self):
 34        ts = TimeStamp(12.3456789)
 35        self.assertEqual(ts.seconds, 12)
 36        self.assertEqual(ts.micros, 345679)
 37
 38        ts = TimeStamp(123.456789)
 39        self.assertEqual(ts.seconds, 123)
 40        self.assertEqual(ts.micros, 456789)
 41
 42        ts = TimeStamp(1234.56789)
 43        self.assertEqual(ts.seconds, 1234)
 44        self.assertEqual(ts.micros, 567890)
 45
 46        ts = TimeStamp(12345.6789)
 47        self.assertEqual(ts.seconds, 12345)
 48        self.assertEqual(ts.micros, 678900)
 49
 50        ts = TimeStamp(123456.789)
 51        self.assertEqual(ts.seconds, 123456)
 52        self.assertEqual(ts.micros, 789000)
 53
 54        ts = TimeStamp(1234567.89)
 55        self.assertEqual(ts.seconds, 1234567)
 56        self.assertEqual(ts.micros, 890000)
 57
 58        ts = TimeStamp(12345678.9)
 59        self.assertEqual(ts.seconds, 12345678)
 60        self.assertEqual(ts.micros, 900000)
 61
 62        ts = TimeStamp(123456789.)
 63        self.assertEqual(ts.seconds, 123456789)
 64        self.assertEqual(ts.micros, 0)
 65        
 66    def testIntConstruction(self):
 67        ts = TimeStamp(123456789)
 68        self.assertEqual(ts.seconds, 123456789)
 69        self.assertEqual(ts.micros, 0)
 70
 71        ts = TimeStamp(123, 456789)
 72        self.assertEqual(ts.seconds, 123)
 73        self.assertEqual(ts.micros, 456789)
 74
 75    def testStringConstruction(self):
 76        ts = TimeStamp('12.3456789')
 77        self.assertEqual(ts.seconds, 12)
 78        self.assertEqual(ts.micros, 345679)
 79
 80        ts = TimeStamp('123.456789')
 81        self.assertEqual(ts.seconds, 123)
 82        self.assertEqual(ts.micros, 456789)
 83
 84        ts = TimeStamp('1234.56789')
 85        self.assertEqual(ts.seconds, 1234)
 86        self.assertEqual(ts.micros, 567890)
 87
 88        ts = TimeStamp('12345.6789')
 89        self.assertEqual(ts.seconds, 12345)
 90        self.assertEqual(ts.micros, 678900)
 91
 92        ts = TimeStamp('123456.789')
 93        self.assertEqual(ts.seconds, 123456)
 94        self.assertEqual(ts.micros, 789000)
 95
 96        ts = TimeStamp('1234567.89')
 97        self.assertEqual(ts.seconds, 1234567)
 98        self.assertEqual(ts.micros, 890000)
 99
100        ts = TimeStamp('12345678.9')
101        self.assertEqual(ts.seconds, 12345678)
102        self.assertEqual(ts.micros, 900000)
103
104        ts = TimeStamp('123456789.')
105        self.assertEqual(ts.seconds, 123456789)
106        self.assertEqual(ts.micros, 0)
107
108
109class TestOperations(unittest.TestCase):
110
111    def testEquality(self):
112        t1 = TimeStamp(100, 900000)
113        t1x = TimeStamp(100.9)
114        self.assert_(t1 == t1x)
115        
116    def testOrdering(self):
117        t1 = TimeStamp(100, 900000)
118        t2 = TimeStamp(100, 900001)
119        t3 = TimeStamp(101, 000002)
120        
121        self.assert_(t1 < t2)
122        self.assert_(t2 < t3)
123        self.assert_(t1 < t3)
124    
125    def testDifference(self):
126        t1 = TimeStamp(100, 900000)
127        t2 = TimeStamp(100, 900001)
128        t3 = TimeStamp(101, 000002)
129        t1x = TimeStamp(100.9)
130
131        self.assertEqual(t1 - t1x, TimeStamp(0,0))
132        
133        self.assertEqual(t2 - t1, TimeStamp(0,1))
134        self.assertEqual(t3 - t2, TimeStamp(0,100001))
135        self.assertEqual(t3 - t1, TimeStamp(0,100002))
136
137        self.assertEqual(t1 - t2, TimeStamp(-1,999999))
138        self.assertEqual(t2 - t3, TimeStamp(-1,899999))
139        self.assertEqual(t1 - t3, TimeStamp(-1,899998))
140
141    def testAdding(self):
142        t1 = TimeStamp(100, 900000)
143        t2 = TimeStamp(100, 900001)
144        t3 = TimeStamp(101, 0)
145        t4 = TimeStamp(101, 000002)
146
147        d0 = TimeStamp(0,0)
148        d21 = TimeStamp(0,1)
149        d31 = TimeStamp(0,100000)
150        d41 = TimeStamp(0,100002)
151        d42 = TimeStamp(0,100001)
152        
153        self.assertEqual(t1 + d0, t1)
154        self.assertEqual(t1 + d21, t2)
155        self.assertEqual(t1 + d31, t3)
156        self.assertEqual(t1 + d41, t4)
157        self.assertEqual(t2 + d42, t4)
158    
159    def testAddInPlace(self):
160        t1 = TimeStamp(100, 900000)
161        t1 += TimeStamp(1,1)
162        self.assertEqual(t1, TimeStamp(101, 900001))
163        
164        
165class TestStringConversion(unittest.TestCase):
166
167    def testStr(self):
168        self.assertEqual(str(TimeStamp(100,0)), '100.000000')
169        self.assertEqual(str(TimeStamp(100,1)), '100.000001')
170        self.assertEqual(str(TimeStamp(0)), '0.000000')
171
172    def testRepr(self):
173        self.assertEqual(repr(TimeStamp(100,0)), 'TimeStamp(100,0)')
174        self.assertEqual(repr(TimeStamp(100,1)), 'TimeStamp(100,1)')
175        self.assertEqual(repr(TimeStamp(0)), 'TimeStamp(0,0)')
176    
177    def testFloat(self):
178        self.assertEqual(float(TimeStamp(100,0)), 100.0)
179        self.assertEqual(float(TimeStamp(100,1)), 100.000001)
180        self.assertEqual(float(TimeStamp(100,499999)), 100.499999)
181        self.assertEqual(float(TimeStamp(100,500000)), 100.500000)
182        self.assertEqual(float(TimeStamp(100,500001)), 100.500001)
183        self.assertEqual(float(TimeStamp(100,999999)), 100.999999)
184        self.assertEqual(float(TimeStamp(0)), 0.0)
185
186class TestRanges(unittest.TestCase):
187    
188    def testBig(self):
189        ts = TimeStamp('1237237351.064861')
190        self.assertEqual(ts.seconds, 1237237351)
191        self.assertEqual(ts.micros, 64861)
192
193
194
195if __name__ == '__main__':
196    unittest.main()