1# Copyright 2018 The Cirq Developers 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from datetime import timedelta 16import pytest 17 18import cirq 19from cirq import Timestamp, Duration 20 21 22def test_init(): 23 assert Timestamp().raw_picos() == 0 24 assert Timestamp(picos=513).raw_picos() == 513 25 assert Timestamp(picos=-5).raw_picos() == -5 26 assert Timestamp(nanos=211).raw_picos() == 211000 27 assert Timestamp(nanos=211, picos=1051).raw_picos() == 212051 28 29 assert isinstance(Timestamp(picos=1).raw_picos(), int) 30 assert isinstance(Timestamp(nanos=1).raw_picos(), int) 31 assert isinstance(Timestamp(picos=1.0).raw_picos(), float) 32 assert isinstance(Timestamp(nanos=1.0).raw_picos(), float) 33 34 35def test_str(): 36 assert str(Timestamp(picos=1000, nanos=1000)) == 't=1001000' 37 assert str(Timestamp(nanos=5.0)) == 't=5000.0' 38 assert str(Timestamp(picos=-100)) == 't=-100' 39 40 41def test_repr(): 42 a = Timestamp(picos=1000, nanos=1000) 43 cirq.testing.assert_equivalent_repr(a) 44 b = Timestamp(nanos=5.0) 45 cirq.testing.assert_equivalent_repr(b) 46 c = Timestamp(picos=-100) 47 cirq.testing.assert_equivalent_repr(c) 48 49 50def test_eq(): 51 eq = cirq.testing.EqualsTester() 52 eq.add_equality_group(Timestamp(), Timestamp(picos=0), Timestamp(nanos=0.0)) 53 eq.add_equality_group(Timestamp(picos=1000), Timestamp(nanos=1)) 54 eq.make_equality_group(lambda: Timestamp(picos=-1)) 55 56 57def test_cmp(): 58 ordered_groups = [ 59 Timestamp(picos=-1), 60 Timestamp(), 61 Timestamp(picos=1), 62 Timestamp(nanos=1), 63 Timestamp(picos=2000), 64 ] 65 66 for i in range(len(ordered_groups)): 67 for j in range(len(ordered_groups)): 68 a = ordered_groups[i] 69 b = ordered_groups[j] 70 assert (i < j) == (a < b) 71 assert (i <= j) == (a <= b) 72 assert (i == j) == (a == b) 73 assert (i != j) == (a != b) 74 assert (i >= j) == (a >= b) 75 assert (i > j) == (a > b) 76 77 assert not (Timestamp() == 0) 78 assert Timestamp() != 0 79 assert not (Timestamp() == Duration()) 80 assert Timestamp() != Duration() 81 82 83def test_cmp_vs_other_type(): 84 with pytest.raises(TypeError): 85 _ = Timestamp() < Duration() 86 with pytest.raises(TypeError): 87 _ = Timestamp() < 0 88 with pytest.raises(TypeError): 89 _ = Timestamp() <= 0 90 with pytest.raises(TypeError): 91 _ = Timestamp() >= 0 92 with pytest.raises(TypeError): 93 _ = Timestamp() > 0 94 95 96def test_add(): 97 assert Timestamp(picos=1) + Duration(picos=2) == Timestamp(picos=3) 98 assert Duration(picos=3) + Timestamp(picos=-5) == Timestamp(picos=-2) 99 100 assert Timestamp(picos=1) + timedelta(microseconds=2) == Timestamp(picos=2000001) 101 assert timedelta(microseconds=3) + Timestamp(picos=-5) == Timestamp(picos=2999995) 102 103 with pytest.raises(TypeError): 104 assert Timestamp() + Timestamp() == Timestamp() 105 with pytest.raises(TypeError): 106 _ = 1 + Timestamp() 107 with pytest.raises(TypeError): 108 _ = Timestamp() + 1 109 110 111def test_sub(): 112 assert Timestamp() - Timestamp() == Duration() 113 assert Timestamp(picos=1) - Timestamp(picos=2) == Duration(picos=-1) 114 assert Timestamp(picos=5) - Duration(picos=2) == Timestamp(picos=3) 115 assert Timestamp(picos=5) - timedelta(microseconds=2) == Timestamp(picos=-1999995) 116 117 with pytest.raises(TypeError): 118 _ = Duration() - Timestamp() 119 with pytest.raises(TypeError): 120 _ = 1 - Timestamp() 121 with pytest.raises(TypeError): 122 _ = Timestamp() - 1 123