mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
added test for quick select
This commit is contained in:
parent
7d6418220e
commit
c88d85b0cb
1 changed files with 38 additions and 0 deletions
38
tests/quick_select_test.cc
Normal file
38
tests/quick_select_test.cc
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
Test for the Quick select algorithm
|
||||
|
||||
Copyright 2024 Ahmet Inan <inan@aicodix.de>
|
||||
*/
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include "quick_select.hh"
|
||||
|
||||
int main()
|
||||
{
|
||||
const int MAX_N = 1 << 16;
|
||||
unsigned seed = 42;
|
||||
if (1) {
|
||||
std::random_device rd;
|
||||
seed = rd();
|
||||
}
|
||||
typedef std::default_random_engine generator;
|
||||
typedef std::uniform_int_distribution<int> distribution;
|
||||
auto rand = std::bind(distribution(1, MAX_N), generator(seed));
|
||||
int a[MAX_N], b[MAX_N];
|
||||
for (int loop = 0; loop < 10000; ++loop) {
|
||||
int size = rand();
|
||||
for (int i = 0; i < size; ++i)
|
||||
a[i] = b[i] = rand();
|
||||
int k = rand() % size;
|
||||
std::nth_element(a, a+k, a+size);
|
||||
int v = DSP::quick_select(b, k, size);
|
||||
assert(v == a[k]);
|
||||
}
|
||||
std::cerr << "Quick select algorithm test passed!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue