~ubuntu-branches/ubuntu/natty/python3.2/natty-updates

« back to all changes in this revision

Viewing changes to Lib/test/test_time.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-01-16 18:09:56 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110116180956-yf91p6ode1z6lsi1
Tags: 3.2~rc1-0ubuntu1
Python 3.2 release candidate 1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
import time
3
3
import unittest
4
4
import locale
 
5
import sysconfig
 
6
import warnings
5
7
 
6
8
class TimeTestCase(unittest.TestCase):
7
9
 
18
20
        time.clock()
19
21
 
20
22
    def test_conversions(self):
21
 
        self.assertTrue(time.ctime(self.t)
22
 
                     == time.asctime(time.localtime(self.t)))
23
 
        self.assertTrue(int(time.mktime(time.localtime(self.t)))
24
 
                     == int(self.t))
 
23
        self.assertEqual(time.ctime(self.t),
 
24
                         time.asctime(time.localtime(self.t)))
 
25
        self.assertEqual(int(time.mktime(time.localtime(self.t))),
 
26
                         int(self.t))
25
27
 
26
28
    def test_sleep(self):
27
29
        time.sleep(1.2)
41
43
        # Make sure that strftime() checks the bounds of the various parts
42
44
        #of the time tuple (0 is valid for *all* values).
43
45
 
44
 
        # Check year [1900, max(int)]
45
 
        self.assertRaises(ValueError, func,
46
 
                            (1899, 1, 1, 0, 0, 0, 0, 1, -1))
47
 
        if time.accept2dyear:
48
 
            self.assertRaises(ValueError, func,
49
 
                                (-1, 1, 1, 0, 0, 0, 0, 1, -1))
50
 
            self.assertRaises(ValueError, func,
51
 
                                (100, 1, 1, 0, 0, 0, 0, 1, -1))
 
46
        # The year field is tested by other test cases above
 
47
 
52
48
        # Check month [1, 12] + zero support
53
49
        self.assertRaises(ValueError, func,
54
50
                            (1900, -1, 1, 0, 0, 0, 0, 1, -1))
96
92
        # No test for daylight savings since strftime() does not change output
97
93
        # based on its value.
98
94
        expected = "2000 01 01 00 00 00 1 001"
99
 
        result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
 
95
        with support.check_warnings():
 
96
            result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
100
97
        self.assertEqual(expected, result)
101
98
 
102
99
    def test_strptime(self):
121
118
 
122
119
    def test_asctime(self):
123
120
        time.asctime(time.gmtime(self.t))
 
121
 
 
122
        # Max year is only limited by the size of C int.
 
123
        sizeof_int = sysconfig.get_config_var('SIZEOF_INT') or 4
 
124
        bigyear = (1 << 8 * sizeof_int - 1) - 1
 
125
        asc = time.asctime((bigyear, 6, 1) + (0,)*6)
 
126
        self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
 
127
        self.assertRaises(OverflowError, time.asctime, (bigyear + 1,) + (0,)*8)
124
128
        self.assertRaises(TypeError, time.asctime, 0)
 
129
        self.assertRaises(TypeError, time.asctime, ())
 
130
        self.assertRaises(TypeError, time.asctime, (0,) * 10)
125
131
 
126
132
    def test_asctime_bounding_check(self):
127
133
        self._bounds_checking(time.asctime)
128
134
 
 
135
    def test_ctime(self):
 
136
        t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
 
137
        self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
 
138
        t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
 
139
        self.assertEqual(time.ctime(t), 'Sat Jan  1 00:00:00 2000')
 
140
        for year in [-100, 100, 1000, 2000, 10000]:
 
141
            try:
 
142
                testval = time.mktime((year, 1, 10) + (0,)*6)
 
143
            except (ValueError, OverflowError):
 
144
                # If mktime fails, ctime will fail too.  This may happen
 
145
                # on some platforms.
 
146
                pass
 
147
            else:
 
148
                self.assertEqual(time.ctime(testval)[20:], str(year))
 
149
 
129
150
    @unittest.skipIf(not hasattr(time, "tzset"),
130
151
        "time module has no attribute tzset")
131
152
    def test_tzset(self):
215
236
        gt1 = time.gmtime(None)
216
237
        t0 = time.mktime(gt0)
217
238
        t1 = time.mktime(gt1)
218
 
        self.assertTrue(0 <= (t1-t0) < 0.2)
 
239
        self.assertAlmostEqual(t1, t0, delta=0.2)
219
240
 
220
241
    def test_localtime_without_arg(self):
221
242
        lt0 = time.localtime()
222
243
        lt1 = time.localtime(None)
223
244
        t0 = time.mktime(lt0)
224
245
        t1 = time.mktime(lt1)
225
 
        self.assertTrue(0 <= (t1-t0) < 0.2)
 
246
        self.assertAlmostEqual(t1, t0, delta=0.2)
226
247
 
227
248
class TestLocale(unittest.TestCase):
228
249
    def setUp(self):
240
261
        # This should not cause an exception
241
262
        time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
242
263
 
 
264
 
 
265
class _BaseYearTest(unittest.TestCase):
 
266
    accept2dyear = None
 
267
 
 
268
    def setUp(self):
 
269
        self.saved_accept2dyear = time.accept2dyear
 
270
        time.accept2dyear = self.accept2dyear
 
271
 
 
272
    def tearDown(self):
 
273
        time.accept2dyear = self.saved_accept2dyear
 
274
 
 
275
    def yearstr(self, y):
 
276
        raise NotImplementedError()
 
277
 
 
278
class _TestAsctimeYear:
 
279
    def yearstr(self, y):
 
280
        return time.asctime((y,) + (0,) * 8).split()[-1]
 
281
 
 
282
    def test_large_year(self):
 
283
        # Check that it doesn't crash for year > 9999
 
284
        self.assertEqual(self.yearstr(12345), '12345')
 
285
        self.assertEqual(self.yearstr(123456789), '123456789')
 
286
 
 
287
class _TestStrftimeYear:
 
288
    def yearstr(self, y):
 
289
        return time.strftime('%Y', (y,) + (0,) * 8).split()[-1]
 
290
 
 
291
    def test_large_year(self):
 
292
        # Check that it doesn't crash for year > 9999
 
293
        try:
 
294
            text = self.yearstr(12345)
 
295
        except ValueError:
 
296
            # strftime() is limited to [1; 9999] with Visual Studio
 
297
            return
 
298
        # Issue #10864: OpenIndiana is limited to 4 digits,
 
299
        # but Python doesn't raise a ValueError
 
300
        #self.assertEqual(text, '12345')
 
301
        #self.assertEqual(self.yearstr(123456789), '123456789')
 
302
        self.assertIn(text, ('2345', '12345'))
 
303
        self.assertIn(self.yearstr(123456789), ('123456789', '6789'))
 
304
 
 
305
class _Test2dYear(_BaseYearTest):
 
306
    accept2dyear = 1
 
307
 
 
308
    def test_year(self):
 
309
        with support.check_warnings():
 
310
            self.assertEqual(self.yearstr(0), '2000')
 
311
            self.assertEqual(self.yearstr(69), '1969')
 
312
            self.assertEqual(self.yearstr(68), '2068')
 
313
            self.assertEqual(self.yearstr(99), '1999')
 
314
 
 
315
    def test_invalid(self):
 
316
        self.assertRaises(ValueError, self.yearstr, -1)
 
317
        self.assertRaises(ValueError, self.yearstr, 100)
 
318
        self.assertRaises(ValueError, self.yearstr, 999)
 
319
 
 
320
class _Test4dYear(_BaseYearTest):
 
321
    accept2dyear = 0
 
322
 
 
323
    def test_year(self):
 
324
        self.assertIn(self.yearstr(1),     ('1', '0001'))
 
325
        self.assertIn(self.yearstr(68),   ('68', '0068'))
 
326
        self.assertIn(self.yearstr(69),   ('69', '0069'))
 
327
        self.assertIn(self.yearstr(99),   ('99', '0099'))
 
328
        self.assertIn(self.yearstr(999), ('999', '0999'))
 
329
        self.assertEqual(self.yearstr(9999), '9999')
 
330
 
 
331
    def test_negative(self):
 
332
        try:
 
333
            text = self.yearstr(-1)
 
334
        except ValueError:
 
335
            # strftime() is limited to [1; 9999] with Visual Studio
 
336
            return
 
337
        self.assertIn(text, ('-1', '-001'))
 
338
 
 
339
        self.assertEqual(self.yearstr(-1234), '-1234')
 
340
        self.assertEqual(self.yearstr(-123456), '-123456')
 
341
 
 
342
 
 
343
    def test_mktime(self):
 
344
        # Issue #1726687
 
345
        for t in (-2, -1, 0, 1):
 
346
            try:
 
347
                tt = time.localtime(t)
 
348
            except (OverflowError, ValueError):
 
349
                pass
 
350
            else:
 
351
                self.assertEqual(time.mktime(tt), t)
 
352
        # It may not be possible to reliably make mktime return error
 
353
        # on all platfom.  This will make sure that no other exception
 
354
        # than OverflowError is raised for an extreme value.
 
355
        try:
 
356
            time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
 
357
        except OverflowError:
 
358
            pass
 
359
 
 
360
class TestAsctimeAccept2dYear(_TestAsctimeYear, _Test2dYear):
 
361
    pass
 
362
 
 
363
class TestStrftimeAccept2dYear(_TestStrftimeYear, _Test2dYear):
 
364
    pass
 
365
 
 
366
class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear):
 
367
    pass
 
368
 
 
369
class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear):
 
370
    pass
 
371
 
 
372
class Test2dyearBool(_TestAsctimeYear, _Test2dYear):
 
373
    accept2dyear = True
 
374
 
 
375
class Test4dyearBool(_TestAsctimeYear, _Test4dYear):
 
376
    accept2dyear = False
 
377
 
 
378
class TestAccept2YearBad(_TestAsctimeYear, _BaseYearTest):
 
379
    class X:
 
380
        def __bool__(self):
 
381
            raise RuntimeError('boo')
 
382
    accept2dyear = X()
 
383
    def test_2dyear(self):
 
384
        pass
 
385
    def test_invalid(self):
 
386
        self.assertRaises(RuntimeError, self.yearstr, 200)
 
387
 
 
388
 
243
389
def test_main():
244
 
    support.run_unittest(TimeTestCase, TestLocale)
 
390
    support.run_unittest(
 
391
        TimeTestCase,
 
392
        TestLocale,
 
393
        TestAsctimeAccept2dYear,
 
394
        TestStrftimeAccept2dYear,
 
395
        TestAsctime4dyear,
 
396
        TestStrftime4dyear,
 
397
        Test2dyearBool,
 
398
        Test4dyearBool,
 
399
        TestAccept2YearBad)
245
400
 
246
401
if __name__ == "__main__":
247
402
    test_main()