unit: make multiaccel tests deterministic

Use a PRNG, not rand().
This commit is contained in:
Justin Viiret 2016-06-07 12:00:30 +10:00 committed by Matthew Barr
parent 8e4c68e9df
commit ea62ba107f

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Intel Corporation
* Copyright (c) 2015-2016, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -43,47 +43,16 @@ extern "C" {
#include "util/alloc.h"
#include "util/charreach.h"
#include <algorithm>
#include <iostream>
#include <random>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <iostream>
using namespace ue2;
using namespace std;
using namespace testing;
/*
* Static functions needed for this test's wellbeing
*/
// char generator
static inline
char getChar(const CharReach &cr, bool match) {
char result;
do {
result = rand() % CharReach::npos;
} while (cr.test(result) != match);
return result;
}
// appends a string with matches/unmatches according to input match pattern
static
void getMatch(u8 *result, u32 start, const string &pattern,
const CharReach &cr) {
for (const auto &c : pattern) {
result[start++] = getChar(cr, c == '1');
}
}
// appends non-matching noise of certain lengths
static
void getNoise(u8 *result, u32 start, u32 len, const CharReach &cr) {
for (unsigned i = 0; i < len; i++) {
result[start + i] = getChar(cr, false);
}
}
// test parameters structure
struct MultiaccelTestParam {
string match_pattern;
@ -126,6 +95,34 @@ protected:
test_all_offsets = p.test_all_offsets;
}
char getChar(const CharReach &cr) {
assert(cr.count() > 0);
auto dist = uniform_int_distribution<size_t>(0, cr.count() - 1);
size_t result = cr.find_nth(dist(prng));
assert(result != CharReach::npos);
return (char)result;
}
// char generator
char getChar(const CharReach &cr, bool match) {
return getChar(match ? cr : ~cr);
}
// appends a string with matches/unmatches according to input match pattern
void getMatch(u8 *result, u32 start, const string &pattern,
const CharReach &cr) {
for (const auto &c : pattern) {
result[start++] = getChar(cr, c == '1');
}
}
// appends non-matching noise of certain lengths
void getNoise(u8 *result, u32 start, u32 len, const CharReach &cr) {
for (unsigned i = 0; i < len; i++) {
result[start + i] = getChar(cr, false);
}
}
// deferred buffer generation, as we don't know CharReach before we run the test
void GenerateBuffer(const CharReach &cr) {
const MultiaccelTestParam &p = GetParam();
@ -167,6 +164,10 @@ protected:
aligned_free(buffer);
}
// We want our tests to be deterministic, so we use a PRNG in the test
// fixture.
mt19937 prng;
u32 match_idx;
u8 *buffer;
bool test_all_offsets;