]>
git.proxmox.com Git - mirror_frr.git/blob - tests/helpers/c/prng.c
2 * Very simple prng to allow for randomized tests with reproducable
5 * Copyright (C) 2012 by Open Source Routing.
6 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
8 * This file is part of Quagga
10 * Quagga is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
15 * Quagga is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Quagga; see the file COPYING. If not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
35 unsigned long long state1
;
36 unsigned long long state2
;
39 static char prng_bit(struct prng
*prng
)
42 prng
->state1
+= 374441;
43 prng
->state1
%= 1771875;
45 if (prng
->state1
% 2) {
46 prng
->state2
*= 84589;
47 prng
->state2
+= 45989;
48 prng
->state2
%= 217728;
51 return prng
->state2
% 2;
54 struct prng
*prng_new(unsigned long long seed
)
56 struct prng
*rv
= calloc(sizeof(*rv
), 1);
59 rv
->state1
= rv
->state2
= seed
;
64 unsigned int prng_rand(struct prng
*prng
)
66 unsigned int i
, rv
= 0;
68 for (i
= 0; i
< 32; i
++) {
75 const char *prng_fuzz(struct prng
*prng
, const char *string
,
76 const char *charset
, unsigned int operations
)
79 unsigned int charset_len
;
83 unsigned int character
;
85 assert(strlen(string
) < sizeof(buf
));
87 strncpy(buf
, string
, sizeof(buf
));
88 charset_len
= strlen(charset
);
90 for (i
= 0; i
< operations
; i
++) {
91 offset
= prng_rand(prng
) % strlen(buf
);
92 op
= prng_rand(prng
) % 3;
97 character
= prng_rand(prng
) % charset_len
;
98 buf
[offset
] = charset
[character
];
102 memmove(buf
+ offset
, buf
+ offset
+ 1,
103 strlen(buf
) - offset
);
107 assert(strlen(buf
) + 1 < sizeof(buf
));
109 memmove(buf
+ offset
+ 1, buf
+ offset
,
110 strlen(buf
) + 1 - offset
);
111 character
= prng_rand(prng
) % charset_len
;
112 buf
[offset
] = charset
[character
];
119 void prng_free(struct prng
*prng
)