]>
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 along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 unsigned long long state1
;
36 unsigned long long state2
;
40 prng_bit(struct prng
*prng
)
43 prng
->state1
+= 374441;
44 prng
->state1
%= 1771875;
48 prng
->state2
*= 84589;
49 prng
->state2
+= 45989;
50 prng
->state2
%= 217728;
53 return prng
->state2
% 2;
57 prng_new(unsigned long long seed
)
59 struct prng
*rv
= calloc(sizeof(*rv
), 1);
62 rv
->state1
= rv
->state2
= seed
;
68 prng_rand(struct prng
*prng
)
70 unsigned int i
, rv
= 0;
72 for (i
= 0; i
< 32; i
++)
81 prng_fuzz(struct prng
*prng
,
84 unsigned int operations
)
87 unsigned int charset_len
;
91 unsigned int character
;
93 assert(strlen(string
) < sizeof(buf
));
95 strncpy(buf
, string
, sizeof(buf
));
96 charset_len
= strlen(charset
);
98 for (i
= 0; i
< operations
; i
++)
100 offset
= prng_rand(prng
) % strlen(buf
);
101 op
= prng_rand(prng
) % 3;
107 character
= prng_rand(prng
) % charset_len
;
108 buf
[offset
] = charset
[character
];
112 memmove(buf
+ offset
, buf
+ offset
+ 1, strlen(buf
) - offset
);
116 assert(strlen(buf
) + 1 < sizeof(buf
));
118 memmove(buf
+ offset
+ 1, buf
+ offset
, strlen(buf
) + 1 - offset
);
119 character
= prng_rand(prng
) % charset_len
;
120 buf
[offset
] = charset
[character
];
128 prng_free(struct prng
*prng
)