STL_MSFT created this revision.
STL_MSFT added reviewers: EricWF, mclow.lists.
STL_MSFT added a subscriber: cfe-commits.
Fix portability issues in <random> tests.
Three tests were asserting equal() after shuffling a sequence, which assumes
the exact behavior of libc++'s implementation. To be portable, yet retain some
level of validation, I'm marking the equal() checks as libc++ specific, but
adding unconditional is_permutation() checks.
Additionally, one test was assuming libc++'s choice of default_random_engine,
which isn't guaranteed by the Standard. Mark that assert as libc++ specific.
http://reviews.llvm.org/D22155
Files:
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle.pass.cpp
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_urng.pass.cpp
test/std/numerics/rand/rand.predef/default_random_engine.pass.cpp
Index: test/std/numerics/rand/rand.predef/default_random_engine.pass.cpp
===================================================================
--- test/std/numerics/rand/rand.predef/default_random_engine.pass.cpp
+++ test/std/numerics/rand/rand.predef/default_random_engine.pass.cpp
@@ -14,9 +14,11 @@
#include <random>
#include <cassert>
+#include "test_macros.h"
+
int main()
{
std::default_random_engine e;
e.discard(9999);
- assert(e() == 399268537u);
+ LIBCPP_ASSERT(e() == 399268537u);
}
Index:
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_urng.pass.cpp
===================================================================
---
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_urng.pass.cpp
+++
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_urng.pass.cpp
@@ -17,15 +17,19 @@
#include <random>
#include <cassert>
+#include "test_macros.h"
+
int main()
{
int ia[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int ia1[] = {2, 7, 1, 4, 3, 6, 5, 10, 9, 8};
int ia2[] = {1, 8, 3, 4, 6, 9, 5, 7, 2, 10};
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
std::minstd_rand g;
std::shuffle(ia, ia+sa, g);
- assert(std::equal(ia, ia+sa, ia1));
+ LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
+ assert(std::is_permutation(ia, ia+sa, ia1));
std::shuffle(ia, ia+sa, g);
- assert(std::equal(ia, ia+sa, ia2));
+ LIBCPP_ASSERT(std::equal(ia, ia+sa, ia2));
+ assert(std::is_permutation(ia, ia+sa, ia2));
}
Index:
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp
===================================================================
---
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp
+++
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp
@@ -18,6 +18,8 @@
#include <algorithm>
#include <cassert>
+#include "test_macros.h"
+
struct gen
{
int operator()(int n)
@@ -33,5 +35,6 @@
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
gen r;
std::random_shuffle(ia, ia+sa, r);
- assert(std::equal(ia, ia+sa, ia1));
+ LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
+ assert(std::is_permutation(ia, ia+sa, ia1));
}
Index:
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle.pass.cpp
===================================================================
---
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle.pass.cpp
+++
test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle.pass.cpp
@@ -17,14 +17,18 @@
#include <algorithm>
#include <cassert>
+#include "test_macros.h"
+
int main()
{
int ia[] = {1, 2, 3, 4};
int ia1[] = {1, 4, 3, 2};
int ia2[] = {4, 1, 2, 3};
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
std::random_shuffle(ia, ia+sa);
- assert(std::equal(ia, ia+sa, ia1));
+ LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
+ assert(std::is_permutation(ia, ia+sa, ia1));
std::random_shuffle(ia, ia+sa);
- assert(std::equal(ia, ia+sa, ia2));
+ LIBCPP_ASSERT(std::equal(ia, ia+sa, ia2));
+ assert(std::is_permutation(ia, ia+sa, ia2));
}
Index: test/std/numerics/rand/rand.predef/default_random_engine.pass.cpp
===================================================================
--- test/std/numerics/rand/rand.predef/default_random_engine.pass.cpp
+++ test/std/numerics/rand/rand.predef/default_random_engine.pass.cpp
@@ -14,9 +14,11 @@
#include <random>
#include <cassert>
+#include "test_macros.h"
+
int main()
{
std::default_random_engine e;
e.discard(9999);
- assert(e() == 399268537u);
+ LIBCPP_ASSERT(e() == 399268537u);
}
Index: test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_urng.pass.cpp
===================================================================
--- test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_urng.pass.cpp
+++ test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_urng.pass.cpp
@@ -17,15 +17,19 @@
#include <random>
#include <cassert>
+#include "test_macros.h"
+
int main()
{
int ia[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int ia1[] = {2, 7, 1, 4, 3, 6, 5, 10, 9, 8};
int ia2[] = {1, 8, 3, 4, 6, 9, 5, 7, 2, 10};
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
std::minstd_rand g;
std::shuffle(ia, ia+sa, g);
- assert(std::equal(ia, ia+sa, ia1));
+ LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
+ assert(std::is_permutation(ia, ia+sa, ia1));
std::shuffle(ia, ia+sa, g);
- assert(std::equal(ia, ia+sa, ia2));
+ LIBCPP_ASSERT(std::equal(ia, ia+sa, ia2));
+ assert(std::is_permutation(ia, ia+sa, ia2));
}
Index: test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp
===================================================================
--- test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp
+++ test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp
@@ -18,6 +18,8 @@
#include <algorithm>
#include <cassert>
+#include "test_macros.h"
+
struct gen
{
int operator()(int n)
@@ -33,5 +35,6 @@
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
gen r;
std::random_shuffle(ia, ia+sa, r);
- assert(std::equal(ia, ia+sa, ia1));
+ LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
+ assert(std::is_permutation(ia, ia+sa, ia1));
}
Index: test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle.pass.cpp
===================================================================
--- test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle.pass.cpp
+++ test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle.pass.cpp
@@ -17,14 +17,18 @@
#include <algorithm>
#include <cassert>
+#include "test_macros.h"
+
int main()
{
int ia[] = {1, 2, 3, 4};
int ia1[] = {1, 4, 3, 2};
int ia2[] = {4, 1, 2, 3};
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
std::random_shuffle(ia, ia+sa);
- assert(std::equal(ia, ia+sa, ia1));
+ LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
+ assert(std::is_permutation(ia, ia+sa, ia1));
std::random_shuffle(ia, ia+sa);
- assert(std::equal(ia, ia+sa, ia2));
+ LIBCPP_ASSERT(std::equal(ia, ia+sa, ia2));
+ assert(std::is_permutation(ia, ia+sa, ia2));
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits