]>
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
36 unsigned long long state1
;
37 unsigned long long state2
;
41 prng_bit(struct prng
*prng
)
44 prng
->state1
+= 374441;
45 prng
->state1
%= 1771875;
49 prng
->state2
*= 84589;
50 prng
->state2
+= 45989;
51 prng
->state2
%= 217728;
54 return prng
->state2
% 2;
58 prng_new(unsigned long long seed
)
60 struct prng
*rv
= calloc(sizeof(*rv
), 1);
63 rv
->state1
= rv
->state2
= seed
;
69 prng_rand(struct prng
*prng
)
71 unsigned int i
, rv
= 0;
73 for (i
= 0; i
< 32; i
++)
82 prng_fuzz(struct prng
*prng
,
85 unsigned int operations
)
88 unsigned int charset_len
;
92 unsigned int character
;
94 assert(strlen(string
) < sizeof(buf
));
96 strncpy(buf
, string
, sizeof(buf
));
97 charset_len
= strlen(charset
);
99 for (i
= 0; i
< operations
; i
++)
101 offset
= prng_rand(prng
) % strlen(buf
);
102 op
= prng_rand(prng
) % 3;
108 character
= prng_rand(prng
) % charset_len
;
109 buf
[offset
] = charset
[character
];
113 memmove(buf
+ offset
, buf
+ offset
+ 1, strlen(buf
) - offset
);
117 assert(strlen(buf
) + 1 < sizeof(buf
));
119 memmove(buf
+ offset
+ 1, buf
+ offset
, strlen(buf
) + 1 - offset
);
120 character
= prng_rand(prng
) % charset_len
;
121 buf
[offset
] = charset
[character
];
129 prng_free(struct prng
*prng
)