Skip to content

Commit 487d9ad

Browse files
committed
Merge pull request #13 from spartanPAGE/master
spartanPAGE
2 parents 4fe86ad + fce7c2f commit 487d9ad

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
You need catch framework to compile tests.
2+
Run using this:
3+
"001 - Name Generator - tests" > result.txt -r xml
4+
or similar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#define CATCH_CONFIG_MAIN
2+
#include "catch.hpp"
3+
#include "RandomNameGenerator.hpp"
4+
#include <random>
5+
6+
TEST_CASE("RandomNameGenerator works well without arguments", "[RandomNameGenerator]"){
7+
int seed = 5;
8+
std::default_random_engine re(5);
9+
SECTION("A RandomNameGenerator with \"\" in constructor and 0's in call()"){
10+
RandomNameGenerator<> rng(
11+
RandomNameGenerator<>::Prefixes{""},
12+
RandomNameGenerator<>::Cores{""},
13+
RandomNameGenerator<>::Sufixes{""},
14+
re
15+
);
16+
REQUIRE(rng(0, 0)=="");
17+
}
18+
SECTION("A RandomNameGenerator with empty containers in constructor and 0's in call()"){
19+
RandomNameGenerator<> rng(
20+
RandomNameGenerator<>::Prefixes{},
21+
RandomNameGenerator<>::Cores{},
22+
RandomNameGenerator<>::Sufixes{},
23+
re
24+
);
25+
REQUIRE(rng(0, 0)=="");
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
If you're looking for tests, they're in the "001 - Name Generator - tests" project
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <array>
5+
#include <string>
6+
#include <random>
7+
#include <algorithm>
8+
9+
template<typename RandomEngine = std::default_random_engine>
10+
class RandomNameGenerator{
11+
struct DataCorrector{
12+
void operator()(RandomNameGenerator *rng){
13+
std::for_each(
14+
std::begin(rng->_containers),
15+
std::end(rng->_containers),
16+
[](ContainerType *container){
17+
if(container->empty())
18+
container->push_back("");
19+
}
20+
);
21+
}
22+
};
23+
public:
24+
typedef std::vector<std::string> ContainerType;
25+
typedef ContainerType Prefixes;
26+
typedef ContainerType Cores;
27+
typedef ContainerType Sufixes;
28+
typedef std::string Result;
29+
public:
30+
RandomNameGenerator(
31+
const Prefixes &prefixes,
32+
const Cores &cores,
33+
const Sufixes &sufixes,
34+
RandomEngine &re
35+
): _prefixes(prefixes), _cores(cores), _sufixes(sufixes), _re(re){
36+
constructContainersArray();
37+
DataCorrector()(this);
38+
constructDistros();
39+
}
40+
41+
Result operator()(
42+
size_t minCoreRepeats,
43+
size_t maxCoreRepeats
44+
){
45+
Result result;
46+
Distro coreRepeatsDistribution(minCoreRepeats, maxCoreRepeats);
47+
result += _prefixes[_distros[PrefixID](_re)];
48+
49+
size_t coreRepeats = coreRepeatsDistribution(_re);
50+
while(coreRepeats--)
51+
result += _cores[_distros[CoreID](_re)];
52+
53+
return result += _sufixes[_distros[SufixID](_re)];
54+
}
55+
private:
56+
void constructDistros(){
57+
_distros = std::array<Distro, Count>{
58+
Distro(0, _prefixes.size()-1),
59+
Distro(0, _cores.size()-1),
60+
Distro(0, _sufixes.size()-1)
61+
};
62+
}
63+
void constructContainersArray(){
64+
_containers = std::array<ContainerType *, Count>{
65+
&_prefixes, &_cores, &_sufixes
66+
};
67+
}
68+
private:
69+
typedef std::uniform_int_distribution<int> Distro;
70+
enum{ PrefixID, CoreID, SufixID, Count /* <-- Have to be the last!*/};
71+
std::array<Distro, Count> _distros;
72+
std::array<ContainerType *, Count> _containers;
73+
private:
74+
Prefixes _prefixes;
75+
Cores _cores;
76+
Sufixes _sufixes;
77+
RandomEngine &_re;
78+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "RandomNameGenerator.hpp"
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
int main() {
7+
default_random_engine dre(5);
8+
size_t times, minCores, maxCores;
9+
cin >> times >> minCores >> maxCores;
10+
RandomNameGenerator<> rng(
11+
RandomNameGenerator<>::Prefixes{
12+
"Bal", "Ner",
13+
"Xerth", "Vorc", "Norm",
14+
"Vaz", "Treth", "Melr",
15+
"Jatr", "Urugd", "Petr"
16+
},
17+
RandomNameGenerator<>::Cores{
18+
"",
19+
"adur", "aes", "anim", "apoll", "imac",
20+
"educ", "equis", "extr", "guius", "haun",
21+
"equi", "amora", "hum", "iace", "ille",
22+
"inept", "iuv", "obe", "ocul", "orbis"
23+
},
24+
RandomNameGenerator<>::Sufixes{
25+
"us", "as", "es", "ex",
26+
"aus", "irg", "orex", "or",
27+
"ator", "arox", "urh"
28+
},
29+
dre
30+
);
31+
32+
while(times--)
33+
cout << rng(minCores, maxCores) << endl;
34+
return 0;
35+
}

0 commit comments

Comments
 (0)