xref: /freebsd/contrib/bmake/unit-tests/varmod-order-shuffle.mk (revision 8c973ee23d647bbdebd2c12cb51460d80101e11a)
1*8c973ee2SSimon J. Gerraty# $NetBSD: varmod-order-shuffle.mk,v 1.8 2023/02/26 06:08:06 rillig Exp $
22c3632d1SSimon J. Gerraty#
32c3632d1SSimon J. Gerraty# Tests for the :Ox variable modifier, which returns the words of the
42c3632d1SSimon J. Gerraty# variable, shuffled.
52c3632d1SSimon J. Gerraty#
6e2eeea75SSimon J. Gerraty# The variable modifier :Ox is available since 2005-06-01.
7e2eeea75SSimon J. Gerraty#
82c3632d1SSimon J. Gerraty# As of 2020-08-16, make uses random(3) seeded by the current time in seconds.
9*8c973ee2SSimon J. Gerraty# This makes the random numbers completely predictable since the only other
10*8c973ee2SSimon J. Gerraty# part of make that uses random numbers is the 'randomize-targets' mode, which
11*8c973ee2SSimon J. Gerraty# is off by default.
12956e45f6SSimon J. Gerraty#
13956e45f6SSimon J. Gerraty# Tags: probabilistic
142c3632d1SSimon J. Gerraty
1512904384SSimon J. GerratyWORDS=		one two three four five six seven eight nine ten
162c3632d1SSimon J. Gerraty
172c3632d1SSimon J. Gerraty# Note that 1 in every 10! trials two independently generated
182c3632d1SSimon J. Gerraty# randomized orderings will be the same.  The test framework doesn't
192c3632d1SSimon J. Gerraty# support checking probabilistic output, so we accept that each of the
202c3632d1SSimon J. Gerraty# 3 :Ox tests will incorrectly fail with probability 2.756E-7, which
212c3632d1SSimon J. Gerraty# lets the whole test fail once in 1.209.600 runs, on average.
222c3632d1SSimon J. Gerraty
232c3632d1SSimon J. Gerraty# Create two shuffles using the := assignment operator.
2412904384SSimon J. Gerratyshuffled1:=	${WORDS:Ox}
2512904384SSimon J. Gerratyshuffled2:=	${WORDS:Ox}
262c3632d1SSimon J. Gerraty.if ${shuffled1} == ${shuffled2}
272c3632d1SSimon J. Gerraty.  error ${shuffled1} == ${shuffled2}
282c3632d1SSimon J. Gerraty.endif
292c3632d1SSimon J. Gerraty
302c3632d1SSimon J. Gerraty# Sorting the list before shuffling it has no effect.
3112904384SSimon J. Gerratyshuffled1:=	${WORDS:O:Ox}
3212904384SSimon J. Gerratyshuffled2:=	${WORDS:O:Ox}
332c3632d1SSimon J. Gerraty.if ${shuffled1} == ${shuffled2}
342c3632d1SSimon J. Gerraty.  error ${shuffled1} == ${shuffled2}
352c3632d1SSimon J. Gerraty.endif
362c3632d1SSimon J. Gerraty
372c3632d1SSimon J. Gerraty# Sorting after shuffling must produce the original numbers.
3812904384SSimon J. Gerratysorted:=	${WORDS:Ox:O}
3912904384SSimon J. Gerraty.if ${sorted} != ${WORDS:O}
4012904384SSimon J. Gerraty.  error ${sorted} != ${WORDS:O}
412c3632d1SSimon J. Gerraty.endif
422c3632d1SSimon J. Gerraty
432c3632d1SSimon J. Gerratyall:
44