~aleteoryx/muditaos

ref: e296013b51c6fcdfbb18adcdaf4783a6ff6f5378 muditaos/module-utils/log/tests/RandomStringGenerator.hpp -rw-r--r-- 1.3 KiB
e296013b — Adam Wulkiewicz [BH-1622][BH-1623] Add/fix translations in relaxation 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <random>
#include <string>
#include <vector>

class RandomStringGenerator
{
  public:
    RandomStringGenerator(size_t minLength = minRandomStringLength, size_t maxLength = maxRandomStringLength)
        : minLength(minLength), maxLength(maxLength), lengthDist(minLength, maxLength)
    {}

    std::string getRandomString();
    std::vector<std::string> createRandomStringVector(size_t size);

  private:
    std::uniform_int_distribution<> charDist{0, sizeof(charSet) - 1};
    std::default_random_engine rng{std::random_device{}()};
    const size_t minLength;
    const size_t maxLength;
    std::uniform_int_distribution<> lengthDist;

    static constexpr auto minRandomStringLength = 1;
    static constexpr auto maxRandomStringLength = 25;
    static constexpr char charSet[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
                                       'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                                       'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
                                       'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
};