Home | History | Annotate | Line # | Download | only in gen
t_randomid.c revision 1.5
      1  1.5     isaki /* $NetBSD: t_randomid.c,v 1.5 2015/03/07 09:59:15 isaki Exp $ */
      2  1.1  pgoyette 
      3  1.1  pgoyette /*-
      4  1.1  pgoyette  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1  pgoyette  * All rights reserved.
      6  1.1  pgoyette  *
      7  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
      8  1.1  pgoyette  * modification, are permitted provided that the following conditions
      9  1.1  pgoyette  * are met:
     10  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     15  1.1  pgoyette  *
     16  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  pgoyette  */
     28  1.1  pgoyette 
     29  1.1  pgoyette #include <atf-c.h>
     30  1.1  pgoyette 
     31  1.1  pgoyette #include <sys/types.h>
     32  1.1  pgoyette 
     33  1.1  pgoyette #include <assert.h>
     34  1.1  pgoyette #include <inttypes.h>
     35  1.1  pgoyette #include <randomid.h>
     36  1.1  pgoyette #include <stdio.h>
     37  1.1  pgoyette #include <string.h>
     38  1.1  pgoyette 
     39  1.1  pgoyette #define	PERIOD		30000
     40  1.1  pgoyette 
     41  1.4     isaki uint32_t last[65536];
     42  1.1  pgoyette 
     43  1.3    jruoho ATF_TC(randomid_basic);
     44  1.3    jruoho ATF_TC_HEAD(randomid_basic, tc)
     45  1.1  pgoyette {
     46  1.1  pgoyette 
     47  1.1  pgoyette 	atf_tc_set_md_var(tc, "descr", "Check randomid(3)");
     48  1.1  pgoyette }
     49  1.1  pgoyette 
     50  1.3    jruoho ATF_TC_BODY(randomid_basic, tc)
     51  1.1  pgoyette {
     52  1.1  pgoyette 	static randomid_t ctx = NULL;
     53  1.4     isaki 	uint32_t lowest, n, diff;
     54  1.1  pgoyette 	uint16_t id;
     55  1.1  pgoyette 
     56  1.1  pgoyette 	memset(last, 0, sizeof(last));
     57  1.1  pgoyette 	ctx = randomid_new(16, (long)3600);
     58  1.1  pgoyette 
     59  1.4     isaki 	lowest = UINT32_MAX;
     60  1.1  pgoyette 
     61  1.5     isaki 	for (n = 0; n < 100000; n++) {
     62  1.1  pgoyette 		id = randomid(ctx);
     63  1.1  pgoyette 
     64  1.1  pgoyette 		if (last[id] > 0) {
     65  1.1  pgoyette 			diff = n - last[id];
     66  1.1  pgoyette 
     67  1.1  pgoyette 			if (diff <= lowest) {
     68  1.4     isaki 				if (lowest != UINT32_MAX)
     69  1.4     isaki 					printf("id %5d: last call at %9"PRIu32
     70  1.4     isaki 					    ", current call %9"PRIu32
     71  1.4     isaki 					    " (diff %5"PRIu32"), "
     72  1.4     isaki 					    "lowest %"PRIu32"\n",
     73  1.1  pgoyette 					    id, last[id], n, diff, lowest);
     74  1.1  pgoyette 
     75  1.1  pgoyette 				ATF_REQUIRE_MSG(diff >= PERIOD,
     76  1.4     isaki 				    "diff (%"PRIu32") less than minimum "
     77  1.1  pgoyette 				    "period (%d)", diff, PERIOD);
     78  1.1  pgoyette 
     79  1.1  pgoyette 				lowest = diff;
     80  1.1  pgoyette 			}
     81  1.1  pgoyette 		}
     82  1.1  pgoyette 
     83  1.1  pgoyette 		last[id] = n;
     84  1.1  pgoyette 	}
     85  1.1  pgoyette }
     86  1.1  pgoyette 
     87  1.1  pgoyette ATF_TP_ADD_TCS(tp)
     88  1.1  pgoyette {
     89  1.1  pgoyette 
     90  1.3    jruoho 	ATF_TP_ADD_TC(tp, randomid_basic);
     91  1.1  pgoyette 
     92  1.1  pgoyette 	return atf_no_error();
     93  1.1  pgoyette }
     94