xref: /netbsd-src/tests/lib/libc/gen/t_randomid.c (revision 6abe86a4fdc41c20d4bdd8b983526cf5b81b07dc)
1*6abe86a4Sisaki /* $NetBSD: t_randomid.c,v 1.5 2015/03/07 09:59:15 isaki Exp $ */
21c807dbeSpgoyette 
31c807dbeSpgoyette /*-
41c807dbeSpgoyette  * Copyright (c) 2010 The NetBSD Foundation, Inc.
51c807dbeSpgoyette  * All rights reserved.
61c807dbeSpgoyette  *
71c807dbeSpgoyette  * Redistribution and use in source and binary forms, with or without
81c807dbeSpgoyette  * modification, are permitted provided that the following conditions
91c807dbeSpgoyette  * are met:
101c807dbeSpgoyette  * 1. Redistributions of source code must retain the above copyright
111c807dbeSpgoyette  *    notice, this list of conditions and the following disclaimer.
121c807dbeSpgoyette  * 2. Redistributions in binary form must reproduce the above copyright
131c807dbeSpgoyette  *    notice, this list of conditions and the following disclaimer in the
141c807dbeSpgoyette  *    documentation and/or other materials provided with the distribution.
151c807dbeSpgoyette  *
161c807dbeSpgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171c807dbeSpgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181c807dbeSpgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191c807dbeSpgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201c807dbeSpgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211c807dbeSpgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221c807dbeSpgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231c807dbeSpgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241c807dbeSpgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251c807dbeSpgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261c807dbeSpgoyette  * POSSIBILITY OF SUCH DAMAGE.
271c807dbeSpgoyette  */
281c807dbeSpgoyette 
291c807dbeSpgoyette #include <atf-c.h>
301c807dbeSpgoyette 
311c807dbeSpgoyette #include <sys/types.h>
321c807dbeSpgoyette 
331c807dbeSpgoyette #include <assert.h>
341c807dbeSpgoyette #include <inttypes.h>
351c807dbeSpgoyette #include <randomid.h>
361c807dbeSpgoyette #include <stdio.h>
371c807dbeSpgoyette #include <string.h>
381c807dbeSpgoyette 
391c807dbeSpgoyette #define	PERIOD		30000
401c807dbeSpgoyette 
4176ca8ec8Sisaki uint32_t last[65536];
421c807dbeSpgoyette 
439a514225Sjruoho ATF_TC(randomid_basic);
ATF_TC_HEAD(randomid_basic,tc)449a514225Sjruoho ATF_TC_HEAD(randomid_basic, tc)
451c807dbeSpgoyette {
461c807dbeSpgoyette 
471c807dbeSpgoyette 	atf_tc_set_md_var(tc, "descr", "Check randomid(3)");
481c807dbeSpgoyette }
491c807dbeSpgoyette 
ATF_TC_BODY(randomid_basic,tc)509a514225Sjruoho ATF_TC_BODY(randomid_basic, tc)
511c807dbeSpgoyette {
521c807dbeSpgoyette 	static randomid_t ctx = NULL;
5376ca8ec8Sisaki 	uint32_t lowest, n, diff;
541c807dbeSpgoyette 	uint16_t id;
551c807dbeSpgoyette 
561c807dbeSpgoyette 	memset(last, 0, sizeof(last));
571c807dbeSpgoyette 	ctx = randomid_new(16, (long)3600);
581c807dbeSpgoyette 
5976ca8ec8Sisaki 	lowest = UINT32_MAX;
601c807dbeSpgoyette 
61*6abe86a4Sisaki 	for (n = 0; n < 100000; n++) {
621c807dbeSpgoyette 		id = randomid(ctx);
631c807dbeSpgoyette 
641c807dbeSpgoyette 		if (last[id] > 0) {
651c807dbeSpgoyette 			diff = n - last[id];
661c807dbeSpgoyette 
671c807dbeSpgoyette 			if (diff <= lowest) {
6876ca8ec8Sisaki 				if (lowest != UINT32_MAX)
6976ca8ec8Sisaki 					printf("id %5d: last call at %9"PRIu32
7076ca8ec8Sisaki 					    ", current call %9"PRIu32
7176ca8ec8Sisaki 					    " (diff %5"PRIu32"), "
7276ca8ec8Sisaki 					    "lowest %"PRIu32"\n",
731c807dbeSpgoyette 					    id, last[id], n, diff, lowest);
741c807dbeSpgoyette 
751c807dbeSpgoyette 				ATF_REQUIRE_MSG(diff >= PERIOD,
7676ca8ec8Sisaki 				    "diff (%"PRIu32") less than minimum "
771c807dbeSpgoyette 				    "period (%d)", diff, PERIOD);
781c807dbeSpgoyette 
791c807dbeSpgoyette 				lowest = diff;
801c807dbeSpgoyette 			}
811c807dbeSpgoyette 		}
821c807dbeSpgoyette 
831c807dbeSpgoyette 		last[id] = n;
841c807dbeSpgoyette 	}
851c807dbeSpgoyette }
861c807dbeSpgoyette 
ATF_TP_ADD_TCS(tp)871c807dbeSpgoyette ATF_TP_ADD_TCS(tp)
881c807dbeSpgoyette {
891c807dbeSpgoyette 
909a514225Sjruoho 	ATF_TP_ADD_TC(tp, randomid_basic);
911c807dbeSpgoyette 
921c807dbeSpgoyette 	return atf_no_error();
931c807dbeSpgoyette }
94