YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/safe_math-test.cc
Line
Count
Source
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
34
// Must come before gtest.h.
35
#include "yb/gutil/mathlimits.h"
36
37
#include <gtest/gtest.h>
38
#include "yb/util/safe_math.h"
39
40
namespace yb {
41
template<typename T>
42
6
static void DoTest(T a, T b, bool expected) {
43
6
  SCOPED_TRACE(a);
44
6
  SCOPED_TRACE(b);
45
6
  bool overflow = false;
46
6
  T ret = AddWithOverflowCheck(a, b, &overflow);
47
6
  EXPECT_EQ(overflow, expected);
48
6
  if (!overflow) {
49
3
    EXPECT_EQ(ret, a + b);
50
3
  }
51
6
}
safe_math-test.cc:_ZN2ybL6DoTestIiEEvT_S1_b
Line
Count
Source
42
4
static void DoTest(T a, T b, bool expected) {
43
4
  SCOPED_TRACE(a);
44
4
  SCOPED_TRACE(b);
45
4
  bool overflow = false;
46
4
  T ret = AddWithOverflowCheck(a, b, &overflow);
47
4
  EXPECT_EQ(overflow, expected);
48
4
  if (!overflow) {
49
2
    EXPECT_EQ(ret, a + b);
50
2
  }
51
4
}
safe_math-test.cc:_ZN2ybL6DoTestIjEEvT_S1_b
Line
Count
Source
42
2
static void DoTest(T a, T b, bool expected) {
43
2
  SCOPED_TRACE(a);
44
2
  SCOPED_TRACE(b);
45
2
  bool overflow = false;
46
2
  T ret = AddWithOverflowCheck(a, b, &overflow);
47
2
  EXPECT_EQ(overflow, expected);
48
2
  if (!overflow) {
49
1
    EXPECT_EQ(ret, a + b);
50
1
  }
51
2
}
52
53
1
TEST(TestSafeMath, TestSignedInts) {
54
  // Overflow above max of range.
55
1
  DoTest<int32_t>(MathLimits<int32_t>::kMax - 10, 15, true);
56
1
  DoTest<int32_t>(MathLimits<int32_t>::kMax - 10, 10, false);
57
58
  // Underflow around negative
59
1
  DoTest<int32_t>(MathLimits<int32_t>::kMin + 10, -15, true);
60
1
  DoTest<int32_t>(MathLimits<int32_t>::kMin + 10, -5, false);
61
1
}
62
63
1
TEST(TestSafeMath, TestUnsignedInts) {
64
  // Overflow above max
65
1
  DoTest<uint32_t>(MathLimits<uint32_t>::kMax - 10, 15, true);
66
1
  DoTest<uint32_t>(MathLimits<uint32_t>::kMax - 10, 10, false);
67
1
}
68
69
} // namespace yb