#!/bin/bash

# Copyright 2021 Stellar Development Foundation and contributors. Licensed
# under the Apache License, Version 2.0. See the COPYING file at the root
# of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

# This file just does some simple static checks that certain obvious causes of
# nondeterminism (between runs, or between compilers / standard libraries) are
# not present in the code.

# $srcdir is set by automake's test driver (pointing at src/ in the source
# tree); fall back to . for direct invocation from src/ in the repo.
if ! cd "${srcdir:-.}"
then
    echo "could not cd to ${srcdir:-.}, cannot check for nondeterminism"
    exit 1
fi

# Check that we're in a git repo, and move to the src/ subdirectory
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1
then
    echo "not in a git repository, cannot check for nondeterminism"
    exit 1
fi
topdir="$(git rev-parse --show-toplevel)"
if ! cd "$topdir/src"
then
    echo "could not cd to src/ in the git repository, cannot check for nondeterminism"
    exit 1
fi

if git grep 'std::rand()' '*.h' '*.cpp'
then
    echo
    echo "please use stellar::getGlobalRandomEngine() instead of std::rand since the latter is not"
    echo "identical on all platforms and other library code may call it unexpectedly."
    exit 1
fi

if git grep 'std::uniform_int_distribution' '*.h' '*.cpp'
then
    echo
    echo "please use stellar::uniform_int_distribution instead of std::uniform_int_distribution"
    echo "since the latter varies behaviour between different C++ standard libraries."
    exit 1
fi

if git grep 'std::shuffle(' '*.h' '*.cpp'
then
    echo
    echo "please use stellar::shuffle instead of std::shuffle since the latter delegates"
    echo "to std::uniform_int_distribution, which varies between compilers."
    exit 1
fi

if git grep 'GENERATE(.*random.*)' '*.h' '*.cpp'
then
    echo
    echo "please do not use Catch2 GENERATE macros with random generators, as they delegate"
    echo "to calls to std::uniform_int_distribution, which varies between compilers."
    exit 1
fi

if git grep 'SecretKey::random' '**/test/*.h' '**/test/*.cpp'
then
    echo
    echo "please use SecretKey::pseudoRandomForTesting() instead of SecretKey::random() in tests"
    exit 1
fi
