start using a .clang-format code style
[m6w6/libmemcached] / test / lib / random.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include <cstddef>
19 #include <chrono>
20 #include <iostream>
21 #include <random>
22 #include <string>
23 #include <type_traits>
24 #include <utility>
25
26 using namespace std;
27
28 using kv_pair = pair<string, string>;
29
30 template<typename T>
31 enable_if_t<is_integral_v<T>, T> random_num(T min, T max) {
32 using namespace chrono;
33 using rnd = mt19937;
34 using dst = uniform_int_distribution<T>;
35
36 static auto time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch());
37 static auto seed = static_cast<rnd::result_type>(time.count() % numeric_limits<T>::max());
38 static auto rgen = rnd{seed};
39
40 return dst(min, max)(rgen);
41 }
42
43 unsigned random_port();
44 string random_port_string(const string &);
45
46 char random_binary();
47 string random_binary_string(size_t len);
48 char random_ascii(char min = '!', char max = '~');
49 string random_ascii_string(size_t len, char min = '!', char max = '~');
50 kv_pair random_ascii_pair(size_t minlen = 1 << 2, size_t maxlen = 1 << 10);
51
52 template<template<typename> class Container>
53 auto random_ascii_pairs(size_t count, size_t minlen = 1 << 2, size_t maxlen = 1 << 10) {
54 Container<kv_pair> v;
55
56 v.reserve(count);
57 for (size_t i = 0; i < count; ++i) {
58 v.emplace_back(random_ascii_pair(minlen, maxlen));
59 }
60
61 return v;
62 }
63
64 string random_socket(const string &prefix = "/tmp/libmc.");
65 string random_socket_or_port_string(const string &what);
66 string random_socket_or_port_flag(const string &binary);
67
68 inline auto random_socket_or_port_arg() {
69 return make_pair(&random_socket_or_port_flag, &random_socket_or_port_string);
70 }