YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/random_util-test.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include "yb/util/random_util.h"
34
35
#include <algorithm>
36
#include <cmath>
37
38
#include "yb/util/random.h"
39
#include "yb/util/test_util.h"
40
41
namespace yb {
42
43
class RandomUtilTest : public YBTest {
44
 protected:
45
1
  RandomUtilTest() : rng_(SeedRandom()) {}
46
47
  Random rng_;
48
49
  static const int kLenMax = 100;
50
  static const int kNumTrials = 100;
51
};
52
53
namespace {
54
55
// Checks string defined at start is set to \0 everywhere but [from, to)
56
101
void CheckEmpty(char* start, int from, int to, int stop) {
57
101
  DCHECK_LE(0, from);
58
101
  DCHECK_LE(from, to);
59
101
  DCHECK_LE(to, stop);
60
7.55k
  for (int j = 0; (j == from ? j = to : j) < stop; ++j) {
61
0
    CHECK_EQ(start[j], '\0') << "Index " << j << " not null after defining"
62
0
                             << "indices [" << from << "," << to << ") of "
63
0
                             << "a nulled string [0," << stop << ").";
64
7.45k
  }
65
101
}
66
67
} // anonymous namespace
68
69
// Makes sure that RandomString only writes the specified amount
70
1
TEST_F(RandomUtilTest, TestRandomString) {
71
1
  char start[kLenMax];
72
73
101
  for (int i = 0; i < kNumTrials; ++i) {
74
100
    memset(start, '\0', kLenMax);
75
100
    int to = rng_.Uniform(kLenMax + 1);
76
100
    int from = rng_.Uniform(to + 1);
77
100
    RandomString(start + from, to - from, &rng_);
78
100
    CheckEmpty(start, from, to, kLenMax);
79
100
  }
80
81
  // Corner case
82
1
  memset(start, '\0', kLenMax);
83
1
  RandomString(start, 0, &rng_);
84
1
  CheckEmpty(start, 0, 0, kLenMax);
85
1
}
86
87
} // namespace yb