Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(945)

Unified Diff: util/numeric/checked_range_test.cc

Issue 467113002: Add CheckedRange<> and its test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Address review feedback Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « util/numeric/checked_range.h ('k') | util/util.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: util/numeric/checked_range_test.cc
diff --git a/util/numeric/checked_range_test.cc b/util/numeric/checked_range_test.cc
new file mode 100644
index 0000000000000000000000000000000000000000..baa843a5ce5f5c152f009fcd8e249848ba538980
--- /dev/null
+++ b/util/numeric/checked_range_test.cc
@@ -0,0 +1,246 @@
+// Copyright 2014 The Crashpad Authors. All rights reserved.
+//
+// Licensed 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 "util/numeric/checked_range.h"
+
+#include <stdint.h>
+
+#include <limits>
+
+#include "base/basictypes.h"
+#include "base/strings/stringprintf.h"
+#include "gtest/gtest.h"
+
+namespace {
+
+using namespace crashpad;
+
+TEST(CheckedRange, IsValid) {
+ const struct UnsignedTestData {
+ uint32_t base;
+ uint32_t size;
+ bool valid;
+ } kUnsignedTestData[] = {
+ {0, 0, true},
+ {0, 1, true},
+ {0, 2, true},
+ {0, 0x7fffffff, true},
+ {0, 0x80000000, true},
+ {0, 0xfffffffe, true},
+ {0, 0xffffffff, true},
+ {1, 0, true},
+ {1, 1, true},
+ {1, 2, true},
+ {1, 0x7fffffff, true},
+ {1, 0x80000000, true},
+ {1, 0xfffffffe, true},
+ {1, 0xffffffff, false},
+ {0x7fffffff, 0, true},
+ {0x7fffffff, 1, true},
+ {0x7fffffff, 2, true},
+ {0x7fffffff, 0x7fffffff, true},
+ {0x7fffffff, 0x80000000, true},
+ {0x7fffffff, 0xfffffffe, false},
+ {0x7fffffff, 0xffffffff, false},
+ {0x80000000, 0, true},
+ {0x80000000, 1, true},
+ {0x80000000, 2, true},
+ {0x80000000, 0x7fffffff, true},
+ {0x80000000, 0x80000000, false},
+ {0x80000000, 0xfffffffe, false},
+ {0x80000000, 0xffffffff, false},
+ {0xfffffffe, 0, true},
+ {0xfffffffe, 1, true},
+ {0xfffffffe, 2, false},
+ {0xfffffffe, 0x7fffffff, false},
+ {0xfffffffe, 0x80000000, false},
+ {0xfffffffe, 0xfffffffe, false},
+ {0xfffffffe, 0xffffffff, false},
+ {0xffffffff, 0, true},
+ {0xffffffff, 1, false},
+ {0xffffffff, 2, false},
+ {0xffffffff, 0x7fffffff, false},
+ {0xffffffff, 0x80000000, false},
+ {0xffffffff, 0xfffffffe, false},
+ {0xffffffff, 0xffffffff, false},
+ };
+
+ for (size_t index = 0; index < arraysize(kUnsignedTestData); ++index) {
+ const UnsignedTestData& testcase = kUnsignedTestData[index];
+ SCOPED_TRACE(base::StringPrintf("unsigned index %zu, base 0x%x, size 0x%x",
+ index,
+ testcase.base,
+ testcase.size));
+
+ CheckedRange<uint32_t> range(testcase.base, testcase.size);
+ EXPECT_EQ(testcase.valid, range.IsValid());
+ }
+
+ const int32_t kMinInt32 = std::numeric_limits<int32_t>::min();
+ const struct SignedTestData {
+ int32_t base;
+ uint32_t size;
+ bool valid;
+ } kSignedTestData[] = {
+ {0, 0, true},
+ {0, 1, true},
+ {0, 2, true},
+ {0, 0x7fffffff, true},
+ {0, 0x80000000, false},
+ {0, 0xfffffffe, false},
+ {0, 0xffffffff, false},
+ {1, 0, true},
+ {1, 1, true},
+ {1, 2, true},
+ {1, 0x7fffffff, false},
+ {1, 0x80000000, false},
+ {1, 0xfffffffe, false},
+ {1, 0xffffffff, false},
+ {0x7fffffff, 0, true},
+ {0x7fffffff, 1, false},
+ {0x7fffffff, 2, false},
+ {0x7fffffff, 0x7fffffff, false},
+ {0x7fffffff, 0x80000000, false},
+ {0x7fffffff, 0xfffffffe, false},
+ {0x7fffffff, 0xffffffff, false},
+ {kMinInt32, 0, true},
+ {kMinInt32, 1, true},
+ {kMinInt32, 2, true},
+ {kMinInt32, 0x7fffffff, true},
+ {kMinInt32, 0x80000000, false},
+ {kMinInt32, 0xfffffffe, false},
+ {kMinInt32, 0xffffffff, false},
+ {-2, 0, true},
+ {-2, 1, true},
+ {-2, 2, true},
+ {-2, 0x7fffffff, true},
+ {-2, 0x80000000, false},
+ {-2, 0xfffffffe, false},
+ {-2, 0xffffffff, false},
+ {-1, 0, true},
+ {-1, 1, true},
+ {-1, 2, true},
+ {-1, 0x7fffffff, true},
+ {-1, 0x80000000, false},
+ {-1, 0xfffffffe, false},
+ {-1, 0xffffffff, false},
+ };
+
+ for (size_t index = 0; index < arraysize(kSignedTestData); ++index) {
+ const SignedTestData& testcase = kSignedTestData[index];
+ SCOPED_TRACE(base::StringPrintf("signed index %zu, base 0x%x, size 0x%x",
+ index,
+ testcase.base,
+ testcase.size));
+
+ CheckedRange<int32_t, uint32_t> range(testcase.base, testcase.size);
+ EXPECT_EQ(testcase.valid, range.IsValid());
+ }
+}
+
+TEST(CheckedRange, ContainsValue) {
+ const struct TestData {
+ uint32_t value;
+ bool valid;
+ } kTestData[] = {
+ {0, false},
+ {1, false},
+ {0x1fff, false},
+ {0x2000, true},
+ {0x2001, true},
+ {0x2ffe, true},
+ {0x2fff, true},
+ {0x3000, false},
+ {0x3001, false},
+ {0x7fffffff, false},
+ {0x80000000, false},
+ {0x80000001, false},
+ {0x80001fff, false},
+ {0x80002000, false},
+ {0x80002001, false},
+ {0x80002ffe, false},
+ {0x80002fff, false},
+ {0x80003000, false},
+ {0x80003001, false},
+ {0xffffcfff, false},
+ {0xffffdfff, false},
+ {0xffffefff, false},
+ {0xffffffff, false},
+ };
+
+ CheckedRange<uint32_t> parent_range(0x2000, 0x1000);
+ ASSERT_TRUE(parent_range.IsValid());
+
+ for (size_t index = 0; index < arraysize(kTestData); ++index) {
+ const TestData& testcase = kTestData[index];
+ SCOPED_TRACE(
+ base::StringPrintf("index %zu, value 0x%x", index, testcase.value));
+
+ EXPECT_EQ(testcase.valid, parent_range.ContainsValue(testcase.value));
+ }
+}
+
+TEST(CheckedRange, ContainsRange) {
+ const struct TestData {
+ uint32_t base;
+ uint32_t size;
+ bool valid;
+ } kTestData[] = {
+ {0, 0, false},
+ {0, 1, false},
+ {0x2000, 0x1000, true},
+ {0, 0x2000, false},
+ {0x3000, 0x1000, false},
+ {0x1800, 0x1000, false},
+ {0x2800, 0x1000, false},
+ {0x2000, 0x800, true},
+ {0x2800, 0x800, true},
+ {0x2400, 0x800, true},
+ {0x2800, 0, true},
+ {0x2000, 0xffffdfff, false},
+ {0x2800, 0xffffd7ff, false},
+ {0x3000, 0xffffcfff, false},
+ {0xfffffffe, 1, false},
+ {0xffffffff, 0, false},
+ {0x1fff, 0, false},
+ {0x2000, 0, true},
+ {0x2001, 0, true},
+ {0x2fff, 0, true},
+ {0x3000, 0, true},
+ {0x3001, 0, false},
+ {0x1fff, 1, false},
+ {0x2000, 1, true},
+ {0x2001, 1, true},
+ {0x2fff, 1, true},
+ {0x3000, 1, false},
+ {0x3001, 1, false},
+ };
+
+ CheckedRange<uint32_t> parent_range(0x2000, 0x1000);
+ ASSERT_TRUE(parent_range.IsValid());
+
+ for (size_t index = 0; index < arraysize(kTestData); ++index) {
+ const TestData& testcase = kTestData[index];
+ SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%x, size 0x%x",
+ index,
+ testcase.base,
+ testcase.size));
+
+ CheckedRange<uint32_t> child_range(testcase.base, testcase.size);
+ ASSERT_TRUE(child_range.IsValid());
+ EXPECT_EQ(testcase.valid, parent_range.ContainsRange(child_range));
+ }
+}
+
+} // namespace
« no previous file with comments | « util/numeric/checked_range.h ('k') | util/util.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698