-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.cpp
executable file
·86 lines (75 loc) · 2.25 KB
/
verify.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <algorithm> // std::binary_search
#include "mergeSort.h"
extern control_block cb;
// Verify against the specified v_file
bool verify(std::vector<int> Keys, int lo, int hi, std::string v_file)
{
std::ifstream f(v_file, std::ios::in | std::ios::binary);
int x;
for (int i = 0 ; i < lo; i++)
f.read((char*)&x, sizeof(int)); // skip these values
for (int i = lo ; i < hi; i++) {
f.read((char*)&x, sizeof(int));
if (x != Keys[i]) {
return false;
}
}
f.close();
return true;
}
//
//
// Sweep over the data verifying that it is correct
// Note that verification of worst and best case input is trival
// as the keys are taken from a simple, known, arithmetic progression.
//
// Random input is different. This test is weak in the sense that, it
// confirms only that the Keys list is sorted, not that the Keys are correct!
//
// For that you need to also check that every input key is found
// in the list of output keys, and you may enable this code
// by uncommenting the line shown below
// Beware, as this test is costly
//
//
bool exactVerify(std::vector<int> Keys, int lo, int hi) {
// Best and worst case are trivial to verify
if (cb.worst){
for (auto i=lo;i<hi; i++) {
if (Keys[i] != i)
return(0);
}
return 1;
}
else if (cb.best){
for (auto i=lo;i<hi; i++) {
if (Keys[i] != i)
return(0);
}
return 1;
}
// If the random case (and the list isn't too long,
// we sort the keys using the C++ built in sort on 1 core
// and compare
// Initialize (seed) the random number generator
srand(cb.sd);
std::vector<int> *KeysE = new std::vector<int>();
assert(KeysE);
for (auto i=0; i< cb.N; i++)
KeysE->push_back(rand());
std::sort(KeysE->begin(),KeysE->begin()+cb.N);
bool areEqual = std::equal(KeysE->begin(),KeysE->begin()+cb.N,Keys.begin());
delete KeysE;
return areEqual;
}
bool weakVerify(std::vector<int> Keys, int lo, int hi) {
for (auto i=lo;i<hi; i++) {
if (Keys[i] > Keys[i+1]){
return(0);
}
}
return 1;
}