From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- mfbt/tests/TestIntegerRange.cpp | 163 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 mfbt/tests/TestIntegerRange.cpp (limited to 'mfbt/tests/TestIntegerRange.cpp') diff --git a/mfbt/tests/TestIntegerRange.cpp b/mfbt/tests/TestIntegerRange.cpp new file mode 100644 index 000000000..c2691a26c --- /dev/null +++ b/mfbt/tests/TestIntegerRange.cpp @@ -0,0 +1,163 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/Assertions.h" +#include "mozilla/IntegerRange.h" + +#include + +using mozilla::IsSame; +using mozilla::MakeRange; +using mozilla::Reversed; + +const size_t kMaxNumber = 50; +const size_t kArraySize = 256; + +template +static IntType +GenerateNumber() +{ + return static_cast(rand() % kMaxNumber + 1); +} + +template +static void +TestSingleParamRange(const IntType aN) +{ + IntType array[kArraySize]; + IntType* ptr = array; + for (auto i : MakeRange(aN)) { + static_assert(IsSame::value, + "type of the loop var and the param should be the same"); + *ptr++ = i; + } + + MOZ_RELEASE_ASSERT(ptr - array == static_cast(aN), + "Should iterates N items"); + for (size_t i = 0; i < static_cast(aN); i++) { + MOZ_RELEASE_ASSERT(array[i] == static_cast(i), + "Values should equal to the index"); + } +} + +template +static void +TestSingleParamReverseRange(const IntType aN) +{ + IntType array[kArraySize]; + IntType* ptr = array; + for (auto i : Reversed(MakeRange(aN))) { + static_assert(IsSame::value, + "type of the loop var and the param should be the same"); + *ptr++ = i; + } + + MOZ_RELEASE_ASSERT(ptr - array == static_cast(aN), + "Should iterates N items"); + for (size_t i = 0; i < static_cast(aN); i++) { + MOZ_RELEASE_ASSERT(array[i] == static_cast(aN - i - 1), + "Values should be the reverse of their index"); + } +} + +template +static void +TestSingleParamIntegerRange() +{ + const auto kN = GenerateNumber(); + TestSingleParamRange(0); + TestSingleParamReverseRange(0); + TestSingleParamRange(kN); + TestSingleParamReverseRange(kN); +} + +template +static void +TestDoubleParamRange(const IntType1 aBegin, const IntType2 aEnd) +{ + IntType2 array[kArraySize]; + IntType2* ptr = array; + for (auto i : MakeRange(aBegin, aEnd)) { + static_assert(IsSame::value, "type of the loop var " + "should be same as that of the second param"); + *ptr++ = i; + } + + MOZ_RELEASE_ASSERT(ptr - array == static_cast(aEnd - aBegin), + "Should iterates (aEnd - aBegin) times"); + for (size_t i = 0; i < static_cast(aEnd - aBegin); i++) { + MOZ_RELEASE_ASSERT(array[i] == static_cast(aBegin + i), + "Should iterate integers in [aBegin, aEnd) in order"); + } +} + +template +static void +TestDoubleParamReverseRange(const IntType1 aBegin, const IntType2 aEnd) +{ + IntType2 array[kArraySize]; + IntType2* ptr = array; + for (auto i : Reversed(MakeRange(aBegin, aEnd))) { + static_assert(IsSame::value, "type of the loop var " + "should be same as that of the second param"); + *ptr++ = i; + } + + MOZ_RELEASE_ASSERT(ptr - array == static_cast(aEnd - aBegin), + "Should iterates (aEnd - aBegin) times"); + for (size_t i = 0; i < static_cast(aEnd - aBegin); i++) { + MOZ_RELEASE_ASSERT(array[i] == static_cast(aEnd - i - 1), + "Should iterate integers in [aBegin, aEnd) in reverse order"); + } +} + +template +static void +TestDoubleParamIntegerRange() +{ + const auto kStart = GenerateNumber(); + const auto kEnd = static_cast(kStart + GenerateNumber()); + TestDoubleParamRange(kStart, static_cast(kStart)); + TestDoubleParamReverseRange(kStart, static_cast(kStart)); + TestDoubleParamRange(kStart, kEnd); + TestDoubleParamReverseRange(kStart, kEnd); +} + +int +main() +{ + TestSingleParamIntegerRange(); + TestSingleParamIntegerRange(); + TestSingleParamIntegerRange(); + TestSingleParamIntegerRange(); + + TestSingleParamIntegerRange(); + TestSingleParamIntegerRange(); + TestSingleParamIntegerRange(); + TestSingleParamIntegerRange(); + + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + TestDoubleParamIntegerRange(); + + return 0; +} -- cgit v1.2.3