Daniel Stone | 5ae7e84 | 2017-03-01 11:34:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016 Collabora, Ltd. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <stdint.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <assert.h> |
| 33 | #include <errno.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | #include "timespec-util.h" |
| 37 | |
| 38 | #include "shared/helpers.h" |
| 39 | #include "zunitc/zunitc.h" |
| 40 | |
| 41 | ZUC_TEST(timespec_test, timespec_sub) |
| 42 | { |
| 43 | struct timespec a, b, r; |
| 44 | |
| 45 | a.tv_sec = 1; |
| 46 | a.tv_nsec = 1; |
| 47 | b.tv_sec = 0; |
| 48 | b.tv_nsec = 2; |
| 49 | timespec_sub(&r, &a, &b); |
| 50 | ZUC_ASSERT_EQ(r.tv_sec, 0); |
| 51 | ZUC_ASSERT_EQ(r.tv_nsec, NSEC_PER_SEC - 1); |
| 52 | } |
| 53 | |
| 54 | ZUC_TEST(timespec_test, timespec_to_nsec) |
| 55 | { |
| 56 | struct timespec a; |
| 57 | |
| 58 | a.tv_sec = 4; |
| 59 | a.tv_nsec = 4; |
| 60 | ZUC_ASSERT_EQ(timespec_to_nsec(&a), (NSEC_PER_SEC * 4ULL) + 4); |
| 61 | } |
| 62 | |
Daniel Stone | 37ad7e3 | 2017-03-01 11:34:02 +0000 | [diff] [blame] | 63 | ZUC_TEST(timespec_test, timespec_to_msec) |
| 64 | { |
| 65 | struct timespec a; |
| 66 | |
| 67 | a.tv_sec = 4; |
| 68 | a.tv_nsec = 4000000; |
| 69 | ZUC_ASSERT_EQ(timespec_to_msec(&a), (4000ULL) + 4); |
| 70 | } |
| 71 | |
Daniel Stone | 5ae7e84 | 2017-03-01 11:34:00 +0000 | [diff] [blame] | 72 | ZUC_TEST(timespec_test, millihz_to_nsec) |
| 73 | { |
| 74 | ZUC_ASSERT_EQ(millihz_to_nsec(60000), 16666666); |
| 75 | } |
| 76 | |
| 77 | ZUC_TEST(timespec_test, timespec_add_nsec) |
| 78 | { |
| 79 | struct timespec a, r; |
| 80 | |
| 81 | a.tv_sec = 0; |
| 82 | a.tv_nsec = NSEC_PER_SEC - 1; |
| 83 | timespec_add_nsec(&r, &a, 1); |
| 84 | ZUC_ASSERT_EQ(1, r.tv_sec); |
| 85 | ZUC_ASSERT_EQ(0, r.tv_nsec); |
| 86 | |
| 87 | timespec_add_nsec(&r, &a, 2); |
| 88 | ZUC_ASSERT_EQ(1, r.tv_sec); |
| 89 | ZUC_ASSERT_EQ(1, r.tv_nsec); |
| 90 | |
| 91 | timespec_add_nsec(&r, &a, (NSEC_PER_SEC * 2ULL)); |
| 92 | ZUC_ASSERT_EQ(2, r.tv_sec); |
| 93 | ZUC_ASSERT_EQ(NSEC_PER_SEC - 1, r.tv_nsec); |
| 94 | |
| 95 | timespec_add_nsec(&r, &a, (NSEC_PER_SEC * 2ULL) + 2); |
| 96 | ZUC_ASSERT_EQ(r.tv_sec, 3); |
| 97 | ZUC_ASSERT_EQ(r.tv_nsec, 1); |
| 98 | |
| 99 | a.tv_sec = 1; |
| 100 | a.tv_nsec = 1; |
| 101 | timespec_add_nsec(&r, &a, -2); |
| 102 | ZUC_ASSERT_EQ(r.tv_sec, 0); |
| 103 | ZUC_ASSERT_EQ(r.tv_nsec, NSEC_PER_SEC - 1); |
| 104 | |
| 105 | a.tv_nsec = 0; |
| 106 | timespec_add_nsec(&r, &a, -NSEC_PER_SEC); |
| 107 | ZUC_ASSERT_EQ(0, r.tv_sec); |
| 108 | ZUC_ASSERT_EQ(0, r.tv_nsec); |
| 109 | |
| 110 | a.tv_nsec = 0; |
| 111 | timespec_add_nsec(&r, &a, -NSEC_PER_SEC + 1); |
| 112 | ZUC_ASSERT_EQ(0, r.tv_sec); |
| 113 | ZUC_ASSERT_EQ(1, r.tv_nsec); |
| 114 | |
| 115 | a.tv_nsec = 50; |
| 116 | timespec_add_nsec(&r, &a, (-NSEC_PER_SEC * 10ULL)); |
| 117 | ZUC_ASSERT_EQ(-9, r.tv_sec); |
| 118 | ZUC_ASSERT_EQ(50, r.tv_nsec); |
| 119 | |
| 120 | r.tv_sec = 4; |
| 121 | r.tv_nsec = 0; |
| 122 | timespec_add_nsec(&r, &r, NSEC_PER_SEC + 10ULL); |
| 123 | ZUC_ASSERT_EQ(5, r.tv_sec); |
| 124 | ZUC_ASSERT_EQ(10, r.tv_nsec); |
| 125 | |
| 126 | timespec_add_nsec(&r, &r, (NSEC_PER_SEC * 3ULL) - 9ULL); |
| 127 | ZUC_ASSERT_EQ(8, r.tv_sec); |
| 128 | ZUC_ASSERT_EQ(1, r.tv_nsec); |
| 129 | |
| 130 | timespec_add_nsec(&r, &r, (NSEC_PER_SEC * 7ULL) + (NSEC_PER_SEC - 1ULL)); |
| 131 | ZUC_ASSERT_EQ(16, r.tv_sec); |
| 132 | ZUC_ASSERT_EQ(0, r.tv_nsec); |
| 133 | } |
Daniel Stone | 61f6d7d | 2017-03-01 11:34:01 +0000 | [diff] [blame] | 134 | |
| 135 | ZUC_TEST(timespec_test, timespec_add_msec) |
| 136 | { |
| 137 | struct timespec a, r; |
| 138 | |
| 139 | a.tv_sec = 1000; |
| 140 | a.tv_nsec = 1; |
| 141 | timespec_add_msec(&r, &a, 2002); |
| 142 | ZUC_ASSERT_EQ(1002, r.tv_sec); |
| 143 | ZUC_ASSERT_EQ(2000001, r.tv_nsec); |
| 144 | } |
Daniel Stone | 839b635 | 2017-03-01 11:34:03 +0000 | [diff] [blame^] | 145 | |
| 146 | ZUC_TEST(timespec_test, timespec_sub_to_nsec) |
| 147 | { |
| 148 | struct timespec a, b; |
| 149 | |
| 150 | a.tv_sec = 1000; |
| 151 | a.tv_nsec = 1; |
| 152 | b.tv_sec = 1; |
| 153 | b.tv_nsec = 2; |
| 154 | ZUC_ASSERT_EQ((999L * NSEC_PER_SEC) - 1, timespec_sub_to_nsec(&a, &b)); |
| 155 | } |
| 156 | |
| 157 | ZUC_TEST(timespec_test, timespec_sub_to_msec) |
| 158 | { |
| 159 | struct timespec a, b; |
| 160 | |
| 161 | a.tv_sec = 1000; |
| 162 | a.tv_nsec = 2000000L; |
| 163 | b.tv_sec = 2; |
| 164 | b.tv_nsec = 1000000L; |
| 165 | ZUC_ASSERT_EQ((998 * 1000) + 1, timespec_sub_to_msec(&a, &b)); |
| 166 | } |