Move random string helpers into a separate file

This commit is contained in:
Max Brunsfeld 2016-07-17 06:22:05 -07:00
parent c3a242740b
commit 285f2272fd
3 changed files with 44 additions and 31 deletions

View file

@ -0,0 +1,35 @@
#include <string>
#include <stdlib.h>
using std::string;
static string random_string(char min, char max) {
string result;
size_t length = random() % 12;
for (size_t i = 0; i < length; i++) {
char inserted_char = min + (random() % (max - min));
result += inserted_char;
}
return result;
}
static string random_char(string characters) {
size_t index = random() % characters.size();
return string() + characters[index];
}
string random_words(size_t count) {
string result;
bool just_inserted_word = false;
for (size_t i = 0; i < count; i++) {
if (random() % 10 < 6) {
result += random_char("!(){}[]<>+-=");
} else {
if (just_inserted_word)
result += " ";
result += random_string('a', 'z');
just_inserted_word = true;
}
}
return result;
}

View file

@ -0,0 +1,8 @@
#ifndef HELPERS_RANDOM_HELPERS_H_
#define HELPERS_RANDOM_HELPERS_H_
#include <string>
std::string random_words(size_t count);
#endif // HELPERS_RANDOM_HELPERS_H_

View file

@ -7,6 +7,7 @@
#include "helpers/point_helpers.h"
#include "helpers/encoding_helpers.h"
#include "helpers/record_alloc.h"
#include "helpers/random_helpers.h"
#include <set>
static void expect_the_correct_tree(TSNode node, TSDocument *document, string tree_string) {
@ -60,37 +61,6 @@ static void expect_a_consistent_tree(TSNode node, TSDocument *document) {
AssertThat(has_changes, Equals(some_child_has_changes));
}
static string random_string(char min, char max) {
string result;
size_t length = random() % 12;
for (size_t i = 0; i < length; i++) {
char inserted_char = min + (random() % (max - min));
result += inserted_char;
}
return result;
}
static string random_char(string characters) {
size_t index = random() % characters.size();
return string() + characters[index];
}
static string random_words(size_t count) {
string result;
bool just_inserted_word = false;
for (size_t i = 0; i < count; i++) {
if (random() % 10 < 6) {
result += random_char("!(){}[]<>+-=");
} else {
if (just_inserted_word)
result += " ";
result += random_string('a', 'z');
just_inserted_word = true;
}
}
return result;
}
START_TEST
describe("The Corpus", []() {