From 76940b2cb2350f6363f3ffe4f83aa8b63b91f5d9 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Fri, 13 Oct 2023 00:27:33 -0400 Subject: [PATCH] Fix a test with a probability of failure. --- Proof/Attestation.test/test.cc | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Proof/Attestation.test/test.cc b/Proof/Attestation.test/test.cc index b495616..6ab951a 100644 --- a/Proof/Attestation.test/test.cc +++ b/Proof/Attestation.test/test.cc @@ -9,6 +9,8 @@ #include +#include + namespace { struct Tester; @@ -154,9 +156,22 @@ main() Sorted::Witness< const std::vector< int > & > sortedVector= Sorter::sort( v ); assert( std::is_sorted( begin( testify( sortedVector ) ), end( testify( sortedVector ) ) ) ); - std::shuffle( begin( v ), end( v ), std::random_device() ); - assert( !std::is_sorted( begin( testify( sortedVector ) ), end( testify( sortedVector ) ) ) ); + // Since this is probability driven, and there's only 5 members, there's some chance that it + // might actually be sorted! Bogosort is a thing, after all. + // + // So instead, I just shuffle until it _isn't_ sorted. Should only repeat once, almost + // all the time. + while( std::is_sorted( begin( testify( sortedVector ) ), end( testify( sortedVector ) ) ) ) + { + std::shuffle( begin( v ), end( v ), std::random_device() ); + } + + if( std::is_sorted( begin( testify( sortedVector ) ), end( testify( sortedVector ) ) ) ) + { + std::cerr << "They were sorted! They shouldn't be!!" << std::endl; + assert( !std::is_sorted( begin( testify( sortedVector ) ), end( testify( sortedVector ) ) ) ); + } std::mutex mtx;