Permalink
Browse files

A script to test Python's hash key determinism.

  • Loading branch information...
Andy Chu
Andy Chu committed Mar 17, 2018
1 parent 920d8c5 commit 9dbeb4b8b90ecb746d9bb1f43692f2434422f34f
Showing with 43 additions and 0 deletions.
  1. +43 −0 opy/determinism.sh
View
@@ -0,0 +1,43 @@
#!/bin/bash
#
# Usage:
# ./determinism.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
dictset() {
local n=${1:-30}
seq $n | python -c '
import sys
d = {}
s = set()
for line in sys.stdin:
i = line.strip()
d[i] = 1
s.add(i)
print "D", " ".join(d.keys())
print "S", " ".join(s)
'
}
# Each iteration is stable.
compare-iters() {
for i in $(seq 10); do
# Run it twice with the same seed
dictset
done
}
# Changing the seed changes the order.
compare-seed() {
for seed in 1 2 3; do
echo "seed = $seed"
# Run it twice with the same seed
PYTHONHASHSEED=$seed $0 dictset
PYTHONHASHSEED=$seed $0 dictset
done
}
"$@"

0 comments on commit 9dbeb4b

Please sign in to comment.