PageRenderTime 113ms CodeModel.GetById 62ms app.highlight 23ms RepoModel.GetById 24ms 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
 26import unittest
 27
 28from apiary.tools.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
 77        ts = TimeStamp('0.123456')
 78        self.assertEqual(ts.seconds, 0)
 79        self.assertEqual(ts.micros, 123456)
 80
 81        ts = TimeStamp('12.3456789')
 82        self.assertEqual(ts.seconds, 12)
 83        self.assertEqual(ts.micros, 345679)
 84
 85        ts = TimeStamp('123.456789')
 86        self.assertEqual(ts.seconds, 123)
 87        self.assertEqual(ts.micros, 456789)
 88
 89        ts = TimeStamp('1234.56789')
 90        self.assertEqual(ts.seconds, 1234)
 91        self.assertEqual(ts.micros, 567890)
 92
 93        ts = TimeStamp('12345.6789')
 94        self.assertEqual(ts.seconds, 12345)
 95        self.assertEqual(ts.micros, 678900)
 96
 97        ts = TimeStamp('123456.789')
 98        self.assertEqual(ts.seconds, 123456)
 99        self.assertEqual(ts.micros, 789000)
100
101        ts = TimeStamp('1234567.89')
102        self.assertEqual(ts.seconds, 1234567)
103        self.assertEqual(ts.micros, 890000)
104
105        ts = TimeStamp('12345678.9')
106        self.assertEqual(ts.seconds, 12345678)
107        self.assertEqual(ts.micros, 900000)
108
109        ts = TimeStamp('123456789.')
110        self.assertEqual(ts.seconds, 123456789)
111        self.assertEqual(ts.micros, 0)
112
113
114class TestOperations(unittest.TestCase):
115
116    def testEquality(self):
117        t1 = TimeStamp(100, 900000)
118        t1x = TimeStamp(100.9)
119        self.assert_(t1 == t1x)
120        
121    def testOrdering(self):
122        t1 = TimeStamp(100, 900000)
123        t2 = TimeStamp(100, 900001)
124        t3 = TimeStamp(101, 000002)
125        
126        self.assert_(t1 < t2)
127        self.assert_(t2 < t3)
128        self.assert_(t1 < t3)
129        self.assert_(t3 > t1)
130    
131    def testDifference(self):
132        t1 = TimeStamp(100, 900000)
133        t2 = TimeStamp(100, 900001)
134        t3 = TimeStamp(101, 000002)
135        t1x = TimeStamp(100.9)
136
137        self.assertEqual(t1 - t1x, TimeStamp(0,0))
138        
139        self.assertEqual(t2 - t1, TimeStamp(0,1))
140        self.assertEqual(t3 - t2, TimeStamp(0,100001))
141        self.assertEqual(t3 - t1, TimeStamp(0,100002))
142
143        self.assertEqual(t1 - t2, TimeStamp(-1,999999))
144        self.assertEqual(t2 - t3, TimeStamp(-1,899999))
145        self.assertEqual(t1 - t3, TimeStamp(-1,899998))
146
147    def testAdding(self):
148        t1 = TimeStamp(100, 900000)
149        t2 = TimeStamp(100, 900001)
150        t3 = TimeStamp(101, 0)
151        t4 = TimeStamp(101, 000002)
152
153        d0 = TimeStamp(0,0)
154        d21 = TimeStamp(0,1)
155        d31 = TimeStamp(0,100000)
156        d41 = TimeStamp(0,100002)
157        d42 = TimeStamp(0,100001)
158        
159        self.assertEqual(t1 + d0, t1)
160        self.assertEqual(t1 + d21, t2)
161        self.assertEqual(t1 + d31, t3)
162        self.assertEqual(t1 + d41, t4)
163        self.assertEqual(t2 + d42, t4)
164    
165    def testAddInPlace(self):
166        t1 = TimeStamp(100, 900000)
167        t1 += TimeStamp(1,1)
168        self.assertEqual(t1, TimeStamp(101, 900001))
169        
170        
171class TestStringConversion(unittest.TestCase):
172
173    def testStr(self):
174        self.assertEqual(str(TimeStamp(100,0)), '100.000000')
175        self.assertEqual(str(TimeStamp(100,1)), '100.000001')
176        self.assertEqual(str(TimeStamp(0)), '0.000000')
177
178    def testRepr(self):
179        self.assertEqual(repr(TimeStamp(100,0)), 'TimeStamp(100,0)')
180        self.assertEqual(repr(TimeStamp(100,1)), 'TimeStamp(100,1)')
181        self.assertEqual(repr(TimeStamp(0)), 'TimeStamp(0,0)')
182    
183    def testFloat(self):
184        self.assertEqual(float(TimeStamp(100,0)), 100.0)
185        self.assertEqual(float(TimeStamp(100,1)), 100.000001)
186        self.assertEqual(float(TimeStamp(100,499999)), 100.499999)
187        self.assertEqual(float(TimeStamp(100,500000)), 100.500000)
188        self.assertEqual(float(TimeStamp(100,500001)), 100.500001)
189        self.assertEqual(float(TimeStamp(100,999999)), 100.999999)
190        self.assertEqual(float(TimeStamp(0)), 0.0)
191
192class TestRanges(unittest.TestCase):
193    
194    def testBig(self):
195        ts = TimeStamp('1237237351.064861')
196        self.assertEqual(ts.seconds, 1237237351)
197        self.assertEqual(ts.micros, 64861)
198
199
200
201if __name__ == '__main__':
202    unittest.main()