1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2010 Couchbase, Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #include "config.h"
19 
20 #include "common.h"
21 #undef NDEBUG
22 
main(void)23 int main(void)
24 {
25    hrtime_t val = 0;
26    cb_assert(hrtime2text(val) == std::string("0 ns"));
27    val = 9;
28    cb_assert(hrtime2text(val) == std::string("9 ns"));
29    val = 99;
30    cb_assert(hrtime2text(val) == std::string("99 ns"));
31    val = 999;
32    cb_assert(hrtime2text(val) == std::string("999 ns"));
33    val = 9999;
34    cb_assert(hrtime2text(val) == std::string("9999 ns"));
35    val *= 1000;
36    cb_assert(hrtime2text(val) == std::string("9999 usec"));
37    val *= 1000;
38    cb_assert(hrtime2text(val) == std::string("9999 ms"));
39    val = 599;
40    val *= 1000*1000*1000;
41    cb_assert(hrtime2text(val) == std::string("599 s"));
42    val = 600;
43    val *= 1000*1000*1000;
44    cb_assert(hrtime2text(val) == std::string("0h:10m:0s"));
45    val = 1;
46    cb_assert(hrtime2text(val) == std::string("1 ns"));
47    val = 10;
48    cb_assert(hrtime2text(val) == std::string("10 ns"));
49    val = 100;
50    cb_assert(hrtime2text(val) == std::string("100 ns"));
51    val = 1000;
52    cb_assert(hrtime2text(val) == std::string("1000 ns"));
53    val = 10000;
54    cb_assert(hrtime2text(val) == std::string("10 usec"));
55    val *= 1000;
56    cb_assert(hrtime2text(val) == std::string("10 ms"));
57    val *= 1000;
58    cb_assert(hrtime2text(val) == std::string("10 s"));
59 
60    hrtime_t now = gethrtime();
61    usleep(200);
62    hrtime_t later = gethrtime();
63    cb_assert(now + 200 < later);
64 
65    return 0;
66 }
67