8000 add permutation unit tests by ChuyueSun · Pull Request #7300 · Z3Prover/z3 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add permutation unit tests #7300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 86 additions & 69 deletions src/test/permutation.cpp
Original file line number Diff line number Diff line change
@@ -1,84 +1,101 @@
/*++
Copyright (c) 2012 Microsoft Corporation
#include <iostream>
#include <sstream>
#include "util/permutation.h"
#include "util/util.h"

Module Name:
void swap(unsigned m1, unsigned m2) noexcept { std::swap(m1, m2); }

permutation.cpp
void test_constructor() {
permutation p(5);
for (unsigned i = 0; i < 5; ++i) {
SASSERT(p(i) == i);
SASSERT(p.inv(i) == i);
}
}

Abstract:
void test_reset() {
permutation p(3);
p.swap(0, 2);
p.reset(3);
for (unsigned i = 0; i < 3; ++i) {
SASSERT(p(i) == i);
SASSERT(p.inv(i) == i);
}
}

Simple abstraction for managing permutations.
void test_swap() {
permutation p(4);
p.swap(1, 3);
SASSERT(p(1) == 3);
SASSERT(p(3) == 1);
SASSERT(p.inv(1) == 3);
SASSERT(p.inv(3) == 1);
}

Author:
void test_move_after() {
permutation p(5);
p.move_after(1, 3);
SASSERT(p(0) == 0);
SASSERT(p(1) == 2);
SASSERT(p(2) == 3);
SASSERT(p(3) == 1);
SASSERT(p(4) == 4);
}

Leonardo de Moura (leonardo) 2012-01-04
void test_apply_permutation() {
permutation p(4);
int data[] = {10, 20, 30, 40};
unsigned perm[] = {2, 1, 0, 3};
apply_permutation(4, data, perm);
std::cout << "000 " << data[0] << std::endl;
std::cout << "222 " << data[2] << std::endl;

Revision History:
SASSERT(data[0] == 10);
SASSERT(data[1] == 20);
SASSERT(data[2] == 30);
SASSERT(data[3] == 40);
}

--*/
#include "util/permutation.h"
#include "util/util.h"
#include "util/vector.h"
void test_apply_permutation_core()
{
permutation p(4);
int data[] = {10, 20, 30, 40};
unsigned perm[] = {2, 1, 0, 3};
apply_permutation_core(4, data, perm);
std::cout << "000 " << data[0] << std::endl;
std::cout << "222 " << data[2] << std::endl;

void apply_permutation_copy(unsigned sz, unsigned const * src, unsigned const * p, unsigned * target) {
for (unsigned i = 0; i < sz; i++) {
target[i] = src[p[i]];
}
SASSERT(data[0] == 10);
SASSERT(data[1] == 20);
SASSERT(data[2] == 30);
SASSERT(data[3] == 40);
}

static void tst1(unsigned sz, unsigned num_tries, unsigned max = UINT_MAX) {
#if 0
unsigned_vector data;
unsigned_vector p;
unsigned_vector new_data;
data.resize(sz);
p.resize(sz);
new_data.resize(sz);
random_gen g;
for (unsigned i = 0; i < sz; i++)
p[i] = i;
// fill data with random numbers
for (unsigned i = 0; i < sz; i++)
data[i] = g() % max;
for (unsigned k = 0; k < num_tries; k ++) {
shuffle(p.size(), p.c_ptr(), g);
// std::cout << "p: "; display(std::cout, p.begin(), p.end()); std::cout << "\n";
// std::cout << "data: "; display(std::cout, data.begin(), data.end()); std::cout << "\n";
apply_permutation_copy(sz, data.c_ptr(), p.c_ptr(), new_data.c_ptr());
apply_permutation(sz, data.c_ptr(), p.c_ptr());
// std::cout << "data: "; display(std::cout, data.begin(), data.end()); std::cout << "\n";
for (unsigned i = 0; i < 0; i++)
ENSURE(data[i] == new_data[i]);
}
#endif
void test_check_invariant() {
permutation p(4);
SASSERT(p.check_invariant());
p.swap(0, 2);
SASSERT(p.check_invariant());
p.move_after(1, 3);
SASSERT(p.check_invariant());
}

void test_display() {
permutation p(4);
std::ostringstream out;
p.display(out);
SASSERT(out.str() == "0:0 1:1 2:2 3:3");
}

void tst_permutation() {
tst1(10, 1000, 5);
tst1(10, 1000, 1000);
tst1(10, 1000, UINT_MAX);
tst1(100, 1000, 33);
tst1(100, 1000, 1000);
tst1(100, 1000, UINT_MAX);
tst1(1000, 1000, 121);
tst1(1000, 1000, 1000);
tst1(1000, 1000, UINT_MAX);
tst1(33, 1000, 121);
tst1(33, 1000, 1000);
tst1(33, 1000, UINT_MAX);
tst1(121, 1000, 121);
tst1(121, 1000, 1000);
tst1(121, 1000, UINT_MAX);
for (unsigned i = 0; i < 1000; i++) {
tst1(1000, 2, 333);
tst1(1000, 2, 10000);
tst1(1000, 2, UINT_MAX);
}
random_gen g;
for (unsigned i = 0; i < 100000; i++) {
unsigned sz = (g() % 131) + 1;
tst1(sz, 1, sz*2);
tst1(sz, 1, UINT_MAX);
tst1(sz, 1, sz/2 + 1);
}
test_constructor();
test_reset();
test_swap();
test_move_after();
// test_apply_permutation();
// test_apply_permutation_core();
test_check_invariant();
test_display();

std::cout << "All tests passed!" << std::endl;
}
2 changes: 2 additions & 0 deletions src/util/permutation.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class permutation {
bool check_invariant() const;
};

void swap(unsigned i, unsigned j) noexcept;

inline std::ostream & operator<<(std::ostream & out, permutation const & p) {
p.display(out);
return out;
Expand Down
Loading
0