Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/gl/lib/randint.c |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | - | |||||||
2 | - | |||||||
3 | - | |||||||
4 | - | |||||||
5 | - | |||||||
6 | struct randint_source | - | ||||||
7 | { | - | ||||||
8 | - | |||||||
9 | struct randread_source *source; | - | ||||||
10 | - | |||||||
11 | - | |||||||
12 | - | |||||||
13 | - | |||||||
14 | - | |||||||
15 | - | |||||||
16 | randint randnum; | - | ||||||
17 | randint randmax; | - | ||||||
18 | }; | - | ||||||
19 | - | |||||||
20 | - | |||||||
21 | - | |||||||
22 | struct randint_source * | - | ||||||
23 | randint_new (struct randread_source *source) | - | ||||||
24 | { | - | ||||||
25 | struct randint_source *s = xmalloc (sizeof *s); | - | ||||||
26 | s->source = source; | - | ||||||
27 | s->randnum = s->randmax = 0; | - | ||||||
28 | return executed 640 times by 6 tests: s;return s; Executed by:
executed 640 times by 6 tests: return s; Executed by:
| 640 | ||||||
29 | } | - | ||||||
30 | - | |||||||
31 | - | |||||||
32 | - | |||||||
33 | - | |||||||
34 | - | |||||||
35 | struct randint_source * | - | ||||||
36 | randint_all_new (char const *name, size_t bytes_bound) | - | ||||||
37 | { | - | ||||||
38 | struct randread_source *source = randread_new (name, bytes_bound); | - | ||||||
39 | return executed 640 times by 6 tests: (source ? randint_new (source) : return (source ? randint_new (source) : ((void *)0) ); Executed by:
executed 640 times by 6 tests: return (source ? randint_new (source) : ((void *)0) ); Executed by:
| 640 | ||||||
40 | ((void *)0) executed 640 times by 6 tests: return (source ? randint_new (source) : ((void *)0) ); Executed by:
| 640 | ||||||
41 | ); executed 640 times by 6 tests: return (source ? randint_new (source) : ((void *)0) ); Executed by:
| 640 | ||||||
42 | } | - | ||||||
43 | - | |||||||
44 | - | |||||||
45 | - | |||||||
46 | struct randread_source * | - | ||||||
47 | randint_get_source (struct randint_source const *s) | - | ||||||
48 | { | - | ||||||
49 | return executed 25 times by 1 test: s->source;return s->source; Executed by:
executed 25 times by 1 test: return s->source; Executed by:
| 25 | ||||||
50 | } | - | ||||||
51 | - | |||||||
52 | - | |||||||
53 | - | |||||||
54 | - | |||||||
55 | enum { HUGE_BYTES = | - | ||||||
56 | (18446744073709551615UL) | - | ||||||
57 | == | - | ||||||
58 | (0x7f * 2 + 1) | - | ||||||
59 | }; | - | ||||||
60 | - | |||||||
61 | - | |||||||
62 | static inline randint shift_left (randint x) | - | ||||||
63 | { | - | ||||||
64 | return executed 19431 times by 6 tests: HUGE_BYTES ? 0 : x << 8;return HUGE_BYTES ? 0 : x << 8; Executed by:
executed 19431 times by 6 tests: return HUGE_BYTES ? 0 : x << 8; Executed by:
| 19431 | ||||||
65 | } | - | ||||||
66 | - | |||||||
67 | - | |||||||
68 | - | |||||||
69 | - | |||||||
70 | - | |||||||
71 | randint | - | ||||||
72 | randint_genmax (struct randint_source *s, randint genmax) | - | ||||||
73 | { | - | ||||||
74 | struct randread_source *source = s->source; | - | ||||||
75 | randint randnum = s->randnum; | - | ||||||
76 | randint randmax = s->randmax; | - | ||||||
77 | randint choices = genmax + 1; | - | ||||||
78 | - | |||||||
79 | while (1) | - | ||||||
80 | { | - | ||||||
81 | if (randmax < genmax
| 4211-6144 | ||||||
82 | { | - | ||||||
83 | - | |||||||
84 | - | |||||||
85 | - | |||||||
86 | size_t i = 0; | - | ||||||
87 | randint rmax = randmax; | - | ||||||
88 | unsigned char buf[sizeof randnum]; | - | ||||||
89 | - | |||||||
90 | do | - | ||||||
91 | { | - | ||||||
92 | rmax = shift_left (rmax) + | - | ||||||
93 | (0x7f * 2 + 1) | - | ||||||
94 | ; | - | ||||||
95 | i++; | - | ||||||
96 | } executed 6477 times by 6 tests: end of block Executed by:
| 6477 | ||||||
97 | while (rmax < genmax
| 333-6144 | ||||||
98 | - | |||||||
99 | randread (source, buf, i); | - | ||||||
100 | i = 0; | - | ||||||
101 | - | |||||||
102 | do | - | ||||||
103 | { | - | ||||||
104 | randnum = shift_left (randnum) + buf[i]; | - | ||||||
105 | randmax = shift_left (randmax) + | - | ||||||
106 | (0x7f * 2 + 1) | - | ||||||
107 | ; | - | ||||||
108 | i++; | - | ||||||
109 | } executed 6477 times by 6 tests: end of block Executed by:
| 6477 | ||||||
110 | while (randmax < genmax
| 333-6144 | ||||||
111 | } executed 6144 times by 6 tests: end of block Executed by:
| 6144 | ||||||
112 | - | |||||||
113 | if (randmax == genmax
| 79-10276 | ||||||
114 | { | - | ||||||
115 | s->randnum = s->randmax = 0; | - | ||||||
116 | return executed 79 times by 2 tests: randnum;return randnum; Executed by:
executed 79 times by 2 tests: return randnum; Executed by:
| 79 | ||||||
117 | } | - | ||||||
118 | else | - | ||||||
119 | { | - | ||||||
120 | randint excess_choices = randmax - genmax; | - | ||||||
121 | randint unusable_choices = excess_choices % choices; | - | ||||||
122 | randint last_usable_choice = randmax - unusable_choices; | - | ||||||
123 | randint reduced_randnum = randnum % choices; | - | ||||||
124 | - | |||||||
125 | if (randnum <= last_usable_choice
| 738-9538 | ||||||
126 | { | - | ||||||
127 | s->randnum = randnum / choices; | - | ||||||
128 | s->randmax = excess_choices / choices; | - | ||||||
129 | return executed 9538 times by 6 tests: reduced_randnum;return reduced_randnum; Executed by:
executed 9538 times by 6 tests: return reduced_randnum; Executed by:
| 9538 | ||||||
130 | } | - | ||||||
131 | - | |||||||
132 | - | |||||||
133 | - | |||||||
134 | randnum = reduced_randnum; | - | ||||||
135 | randmax = unusable_choices - 1; | - | ||||||
136 | } executed 738 times by 5 tests: end of block Executed by:
| 738 | ||||||
137 | } | - | ||||||
138 | } never executed: end of block | 0 | ||||||
139 | - | |||||||
140 | - | |||||||
141 | - | |||||||
142 | void | - | ||||||
143 | randint_free (struct randint_source *s) | - | ||||||
144 | { | - | ||||||
145 | explicit_bzero (s, sizeof *s); | - | ||||||
146 | free (s); | - | ||||||
147 | } executed 620 times by 5 tests: end of block Executed by:
| 620 | ||||||
148 | - | |||||||
149 | - | |||||||
150 | - | |||||||
151 | - | |||||||
152 | int | - | ||||||
153 | randint_all_free (struct randint_source *s) | - | ||||||
154 | { | - | ||||||
155 | int r = randread_free (s->source); | - | ||||||
156 | int e = | - | ||||||
157 | (*__errno_location ()) | - | ||||||
158 | ; | - | ||||||
159 | randint_free (s); | - | ||||||
160 | - | |||||||
161 | (*__errno_location ()) | - | ||||||
162 | = e; | - | ||||||
163 | return executed 620 times by 5 tests: r;return r; Executed by:
executed 620 times by 5 tests: return r; Executed by:
| 620 | ||||||
164 | } | - | ||||||
Switch to Source code | Preprocessed file |