LCOV - code coverage report
Current view: top level - tests/testutils - prng.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 12 31 38.7 %
Date: 2015-11-19 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /*
       2             :  * Very simple prng to allow for randomized tests with reproducable
       3             :  * results.
       4             :  *
       5             :  * Copyright (C) 2012 by Open Source Routing.
       6             :  * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
       7             :  *
       8             :  * This file is part of Quagga
       9             :  *
      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
      13             :  * later version.
      14             :  *
      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.
      19             :  *
      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
      23             :  * 02111-1307, USA.
      24             :  */
      25             : 
      26             : #include <assert.h>
      27             : #include <stdlib.h>
      28             : #include <stdint.h>
      29             : #include <string.h>
      30             : 
      31             : #include "prng.h"
      32             : 
      33             : struct prng
      34             : {
      35             :   uint32_t state;
      36             : };
      37             : 
      38             : struct prng*
      39           2 : prng_new(uint32_t seed)
      40             : {
      41           2 :   struct prng *rv = calloc(sizeof(*rv), 1);
      42           2 :   assert(rv);
      43             : 
      44           2 :   rv->state = seed;
      45             : 
      46           2 :   return rv;
      47             : }
      48             : 
      49             : unsigned int
      50     1008070 : prng_rand(struct prng *prng)
      51             : {
      52     1008070 :   prng->state *= 1664525;
      53     1008070 :   prng->state += 1013904223;
      54             : 
      55     1008070 :   return prng->state;
      56             : }
      57             : 
      58             : const char *
      59           0 : prng_fuzz(struct prng *prng,
      60             :           const char *string,
      61             :           const char *charset,
      62             :           unsigned int operations)
      63             : {
      64             :   static char buf[256];
      65             :   unsigned int charset_len;
      66             :   unsigned int i;
      67             :   unsigned int offset;
      68             :   unsigned int op;
      69             :   unsigned int character;
      70             : 
      71           0 :   assert(strlen(string) < sizeof(buf));
      72             : 
      73           0 :   strncpy(buf, string, sizeof(buf));
      74           0 :   charset_len = strlen(charset);
      75             : 
      76           0 :   for (i = 0; i < operations; i++)
      77             :     {
      78           0 :       offset = prng_rand(prng) % strlen(buf);
      79           0 :       op = prng_rand(prng) % 3;
      80             : 
      81           0 :       switch (op)
      82             :         {
      83             :         case 0:
      84             :           /* replace */
      85           0 :           character = prng_rand(prng) % charset_len;
      86           0 :           buf[offset] = charset[character];
      87           0 :           break;
      88             :         case 1:
      89             :           /* remove */
      90           0 :           memmove(buf + offset, buf + offset + 1, strlen(buf) - offset);
      91           0 :           break;
      92             :         case 2:
      93             :           /* insert */
      94           0 :           assert(strlen(buf) + 1 < sizeof(buf));
      95             : 
      96           0 :           memmove(buf + offset + 1, buf + offset, strlen(buf) + 1 - offset);
      97           0 :           character = prng_rand(prng) % charset_len;
      98           0 :           buf[offset] = charset[character];
      99           0 :           break;
     100             :         }
     101             :     }
     102           0 :   return buf;
     103             : }
     104             : 
     105             : void
     106           2 : prng_free(struct prng *prng)
     107             : {
     108           2 :   free(prng);
     109           2 : }

Generated by: LCOV version 1.10