summaryrefslogtreecommitdiff
path: root/cpp/src/tests/datagen.cpp
blob: acbc07d63c94c25606664ebc850c01677a48e73b (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include <exception>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "qpid/Options.h"

namespace qpid {
namespace tests {

struct Args : public qpid::Options
{
    uint count;
    uint minSize;
    uint maxSize;
    uint minChar;
    uint maxChar;
    bool help;

    Args() : qpid::Options("Random data generator"),
             count(1), minSize(8), maxSize(4096),
             minChar(32), maxChar(126),//safely printable ascii chars
             help(false)
    {
        addOptions()
            ("count", qpid::optValue(count, "N"), "number of data strings to generate")
            ("min-size", qpid::optValue(minSize, "N"), "minimum size of data string")
            ("max-size", qpid::optValue(maxSize, "N"), "maximum size of data string")
            ("min-char", qpid::optValue(minChar, "N"), "minimum char value used in data string")
            ("max-char", qpid::optValue(maxChar, "N"), "maximum char value used in data string")
            ("help", qpid::optValue(help), "print this usage statement");
    }

    bool parse(int argc, char** argv) {
        try {
            qpid::Options::parse(argc, argv);
            if (maxSize < minSize) throw qpid::Options::Exception("max-size must be greater than min-size");
            if (maxChar < minChar) throw qpid::Options::Exception("max-char must be greater than min-char");

            if (help) {
                std::cerr << *this << std::endl << std::endl;
            } else {
                return true;
            }
        } catch (const std::exception& e) {
            std::cerr << *this << std::endl << std::endl << e.what() << std::endl;
        }
        return false;
    }

};

uint random(uint min, uint max)
{
    return (rand() % (max-min+1)) + min;
}

std::string generateData(uint size, uint min, uint max)
{
    std::string data;
    for (uint i = 0; i < size; i++) {
        data += (char) random(min, max);
    }
    return data;
}

}} // namespace qpid::tests

using namespace qpid::tests;

int main(int argc, char** argv)
{
    Args opts;
    if (opts.parse(argc, argv)) {
        srand(time(0));
        for (uint i = 0; i < opts.count; i++) {
            std::cout << generateData(random(opts.minSize, opts.maxSize), opts.minChar, opts.maxChar) << std::endl;
        }
        return 0;
    } else {
        return 1;
    }
}