Skip to content

Commit b6f70c6

Browse files
committed
add random_alnum in testing/util.h/cc
1 parent bbf6b95 commit b6f70c6

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

cpp/src/arrow/io/hdfs_test.cc

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,6 @@
4141
namespace arrow {
4242
namespace io {
4343

44-
std::string RandomPath(const std::string& prefix, int rand_length) {
45-
static const char charset[] =
46-
"0123456789"
47-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
48-
"abcdefghijklmnopqrstuvwxyz";
49-
std::string result = prefix;
50-
result.reserve(prefix.size() + rand_length);
51-
52-
std::mt19937 rng(static_cast<unsigned>(std::time(nullptr)));
53-
std::uniform_int_distribution<> dist(0, sizeof(charset) - 2);
54-
55-
for (int i = 0; i < rand_length; ++i) {
56-
result += charset[dist(rng)];
57-
}
58-
59-
return result;
60-
}
61-
6244
std::vector<uint8_t> RandomData(int64_t size) {
6345
std::vector<uint8_t> buffer(size);
6446
random_bytes(size, 0, buffer.data());
@@ -105,8 +87,12 @@ class TestHadoopFileSystem : public ::testing::Test {
10587

10688
client_ = nullptr;
10789
scratch_dir_ =
108-
(std::filesystem::temp_directory_path() / RandomPath("arrow-hdfs/scratch-", 4))
109-
.string();
90+
(std::filesystem::temp_directory_path() / "arrow-hdfs/scratch-").string();
91+
int random_size = 4;
92+
scratch_dir_.resize(scratch_dir_.size() + random_size, '%');
93+
random_alnum(
94+
random_size, 0,
95+
reinterpret_cast<uint8_t*>(&scratch_dir_[scratch_dir_.size() - random_size]));
11096

11197
loaded_driver_ = false;
11298

cpp/src/arrow/testing/util.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ void random_ascii(int64_t n, uint32_t seed, uint8_t* out) {
9090
rand_uniform_int(n, seed, static_cast<int32_t>('A'), static_cast<int32_t>('z'), out);
9191
}
9292

93+
void random_alnum(int64_t n, uint32_t seed, uint8_t* out) {
94+
static const char charset[] =
95+
"0123456789"
96+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
97+
"abcdefghijklmnopqrstuvwxyz";
98+
pcg32_fast gen(seed);
99+
std::uniform_int_distribution<uint32_t> d(0, sizeof(charset) - 2);
100+
std::generate(out, out + n, [&d, &gen] { return charset[d(gen)]; });
101+
}
102+
93103
int64_t CountNulls(const std::vector<uint8_t>& valid_bytes) {
94104
return static_cast<int64_t>(std::count(valid_bytes.cbegin(), valid_bytes.cend(), '\0'));
95105
}

cpp/src/arrow/testing/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ ARROW_TESTING_EXPORT void random_bytes(int64_t n, uint32_t seed, uint8_t* out);
6464
ARROW_TESTING_EXPORT std::string random_string(int64_t n, uint32_t seed);
6565
ARROW_TESTING_EXPORT int32_t DecimalSize(int32_t precision);
6666
ARROW_TESTING_EXPORT void random_ascii(int64_t n, uint32_t seed, uint8_t* out);
67+
ARROW_TESTING_EXPORT void random_alnum(int64_t n, uint32_t seed, uint8_t* out);
6768
ARROW_TESTING_EXPORT int64_t CountNulls(const std::vector<uint8_t>& valid_bytes);
6869

6970
ARROW_TESTING_EXPORT Status MakeRandomByteBuffer(int64_t length, MemoryPool* pool,

0 commit comments

Comments
 (0)