1 /*
2  * iperf, Copyright (c) 2014, 2017, The Regents of the University of
3  * California, through Lawrence Berkeley National Laboratory (subject
4  * to receipt of any required approvals from the U.S. Dept. of
5  * Energy).  All rights reserved.
6  *
7  * If you have questions about your rights to use or distribute this
8  * software, please contact Berkeley Lab's Technology Transfer
9  * Department at TTD@lbl.gov.
10  *
11  * NOTICE.  This software is owned by the U.S. Department of Energy.
12  * As such, the U.S. Government has been granted for itself and others
13  * acting on its behalf a paid-up, nonexclusive, irrevocable,
14  * worldwide license in the Software to reproduce, prepare derivative
15  * works, and perform publicly and display publicly.  Beginning five
16  * (5) years after the date permission to assert copyright is obtained
17  * from the U.S. Department of Energy, and subject to any subsequent
18  * five (5) year renewals, the U.S. Government is granted for itself
19  * and others acting on its behalf a paid-up, nonexclusive,
20  * irrevocable, worldwide license in the Software to reproduce,
21  * prepare derivative works, distribute copies to the public, perform
22  * publicly and display publicly, and to permit others to do so.
23  *
24  * This code is distributed under a BSD style license, see the LICENSE
25  * file for complete information.
26  */
27 #include <assert.h>
28 #ifdef HAVE_STDINT_H
29 #include <stdint.h>
30 #endif
31 #include <stdio.h>
32 #include <string.h>
33 
34 #include "iperf.h"
35 #include "units.h"
36 
37 int
main(int argc,char ** argv)38 main(int argc, char **argv)
39 {
40     iperf_size_t llu;
41     double    d;
42     char      s[11];
43 
44     assert(1024.0 * 0.5 == unit_atof("0.5K"));
45     assert(1024.0 == unit_atof("1K"));
46     assert(1024.0 * 1024.0 == unit_atof("1M"));
47     assert(4.0 * 1024.0 * 1024.0 * 1024.0 == unit_atof("4G"));
48     assert(3.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 == unit_atof("3T"));
49 
50     assert(1024.0 * 0.5 == unit_atof("0.5k"));
51     assert(1024.0 == unit_atof("1k"));
52     assert(1024.0 * 1024.0 == unit_atof("1m"));
53     assert(4.0 * 1024.0 * 1024.0 * 1024.0 == unit_atof("4g"));
54     assert(3.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 == unit_atof("3t"));
55 
56     assert(1024 * 0.5 == unit_atoi("0.5K"));
57     assert(1024 == unit_atoi("1K"));
58     assert(1024 * 1024 == unit_atoi("1M"));
59     d = 4.0 * 1024 * 1024 * 1024;
60     llu = (iperf_size_t) d;
61     assert(llu == unit_atoi("4G"));
62     d = 3.0 * 1024 * 1024 * 1024 * 1024;
63     llu = (iperf_size_t) d;
64     assert(llu == unit_atoi("3T"));
65 
66     assert(1024 * 0.5 == unit_atoi("0.5k"));
67     assert(1024 == unit_atoi("1k"));
68     assert(1024 * 1024 == unit_atoi("1m"));
69     d = 4.0 * 1024 * 1024 * 1024;
70     llu = (iperf_size_t) d;
71     assert(llu == unit_atoi("4g"));
72     d = 3.0 * 1024 * 1024 * 1024 * 1024;
73     llu = (iperf_size_t) d;
74     assert(llu == unit_atoi("3t"));
75 
76     unit_snprintf(s, 11, 1024.0, 'A');
77     assert(strncmp(s, "1.00 KByte", 11) == 0);
78 
79     unit_snprintf(s, 11, 1024.0 * 1024.0, 'A');
80     assert(strncmp(s, "1.00 MByte", 11) == 0);
81 
82     unit_snprintf(s, 11, 1000.0, 'k');
83     assert(strncmp(s, "8.00 Kbit", 11) == 0);
84 
85     unit_snprintf(s, 11, 1000.0 * 1000.0, 'a');
86     assert(strncmp(s, "8.00 Mbit", 11) == 0);
87 
88     d = 4.0 * 1024 * 1024 * 1024;
89     unit_snprintf(s, 11, d, 'A');
90     assert(strncmp(s, "4.00 GByte", 11) == 0);
91 
92     unit_snprintf(s, 11, d, 'a');
93     assert(strncmp(s, "34.4 Gbit", 11) == 0);
94 
95     d = 4.0 * 1024 * 1024 * 1024 * 1024;
96     unit_snprintf(s, 11, d, 'A');
97     assert(strncmp(s, "4.00 TByte", 11) == 0);
98 
99     unit_snprintf(s, 11, d, 'a');
100     assert(strncmp(s, "35.2 Tbit", 11) == 0);
101 
102     d = 4.0 * 1024 * 1024 * 1024 * 1024 * 1024;
103     unit_snprintf(s, 11, d, 'A');
104     assert(strncmp(s, "4096 TByte", 11) == 0);
105 
106     unit_snprintf(s, 11, d, 'a');
107     assert(strncmp(s, "36029 Tbit", 11) == 0);
108 
109     return 0;
110 }
111