mfbt/tests/TestCasting.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/Casting.h"
michael@0 7
michael@0 8 #include <stdint.h>
michael@0 9
michael@0 10 using mozilla::BitwiseCast;
michael@0 11 using mozilla::detail::IsInBounds;
michael@0 12
michael@0 13 template<typename Uint, typename Ulong, bool = (sizeof(Uint) == sizeof(Ulong))>
michael@0 14 struct UintUlongBitwiseCast;
michael@0 15
michael@0 16 template<typename Uint, typename Ulong>
michael@0 17 struct UintUlongBitwiseCast<Uint, Ulong, true>
michael@0 18 {
michael@0 19 static void test() {
michael@0 20 MOZ_RELEASE_ASSERT(BitwiseCast<Ulong>(Uint(8675309)) == Ulong(8675309));
michael@0 21 }
michael@0 22 };
michael@0 23
michael@0 24 template<typename Uint, typename Ulong>
michael@0 25 struct UintUlongBitwiseCast<Uint, Ulong, false>
michael@0 26 {
michael@0 27 static void test() { }
michael@0 28 };
michael@0 29
michael@0 30 static void
michael@0 31 TestBitwiseCast()
michael@0 32 {
michael@0 33 MOZ_RELEASE_ASSERT(BitwiseCast<int>(int(8675309)) == int(8675309));
michael@0 34 UintUlongBitwiseCast<unsigned int, unsigned long>::test();
michael@0 35 }
michael@0 36
michael@0 37 static void
michael@0 38 TestSameSize()
michael@0 39 {
michael@0 40 MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int16_t>(int16_t(0))));
michael@0 41 MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int16_t>(int16_t(INT16_MIN))));
michael@0 42 MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int16_t>(int16_t(INT16_MAX))));
michael@0 43 MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, uint16_t>(uint16_t(UINT16_MAX))));
michael@0 44 MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int16_t>(uint16_t(0))));
michael@0 45 MOZ_RELEASE_ASSERT((!IsInBounds<uint16_t, int16_t>(uint16_t(-1))));
michael@0 46 MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint16_t>(int16_t(-1))));
michael@0 47 MOZ_RELEASE_ASSERT((IsInBounds<int16_t, uint16_t>(int16_t(INT16_MAX))));
michael@0 48 MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint16_t>(int16_t(INT16_MIN))));
michael@0 49 MOZ_RELEASE_ASSERT((IsInBounds<int32_t, uint32_t>(int32_t(INT32_MAX))));
michael@0 50 MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint32_t>(int32_t(INT32_MIN))));
michael@0 51 }
michael@0 52
michael@0 53 static void
michael@0 54 TestToBiggerSize()
michael@0 55 {
michael@0 56 MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int32_t>(int16_t(0))));
michael@0 57 MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int32_t>(int16_t(INT16_MIN))));
michael@0 58 MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int32_t>(int16_t(INT16_MAX))));
michael@0 59 MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, uint32_t>(uint16_t(UINT16_MAX))));
michael@0 60 MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int32_t>(uint16_t(0))));
michael@0 61 MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int32_t>(uint16_t(-1))));
michael@0 62 MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint32_t>(int16_t(-1))));
michael@0 63 MOZ_RELEASE_ASSERT((IsInBounds<int16_t, uint32_t>(int16_t(INT16_MAX))));
michael@0 64 MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint32_t>(int16_t(INT16_MIN))));
michael@0 65 MOZ_RELEASE_ASSERT((IsInBounds<int32_t, uint64_t>(int32_t(INT32_MAX))));
michael@0 66 MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint64_t>(int32_t(INT32_MIN))));
michael@0 67 }
michael@0 68
michael@0 69 static void
michael@0 70 TestToSmallerSize()
michael@0 71 {
michael@0 72 MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int8_t>(int16_t(0))));
michael@0 73 MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, int8_t>(int16_t(INT16_MIN))));
michael@0 74 MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, int8_t>(int16_t(INT16_MAX))));
michael@0 75 MOZ_RELEASE_ASSERT((!IsInBounds<uint16_t, uint8_t>(uint16_t(UINT16_MAX))));
michael@0 76 MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int8_t>(uint16_t(0))));
michael@0 77 MOZ_RELEASE_ASSERT((!IsInBounds<uint16_t, int8_t>(uint16_t(-1))));
michael@0 78 MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint8_t>(int16_t(-1))));
michael@0 79 MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint8_t>(int16_t(INT16_MAX))));
michael@0 80 MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint8_t>(int16_t(INT16_MIN))));
michael@0 81 MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint16_t>(int32_t(INT32_MAX))));
michael@0 82 MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint16_t>(int32_t(INT32_MIN))));
michael@0 83
michael@0 84 // Boundary cases
michael@0 85 MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, int32_t>(int64_t(INT32_MIN) - 1)));
michael@0 86 MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MIN))));
michael@0 87 MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MIN) + 1)));
michael@0 88 MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MAX) - 1)));
michael@0 89 MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MAX))));
michael@0 90 MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, int32_t>(int64_t(INT32_MAX) + 1)));
michael@0 91
michael@0 92 MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, uint32_t>(int64_t(-1))));
michael@0 93 MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(0))));
michael@0 94 MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(1))));
michael@0 95 MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(UINT32_MAX) - 1)));
michael@0 96 MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(UINT32_MAX))));
michael@0 97 MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, uint32_t>(int64_t(UINT32_MAX) + 1)));
michael@0 98 }
michael@0 99
michael@0 100 int
michael@0 101 main()
michael@0 102 {
michael@0 103 TestBitwiseCast();
michael@0 104
michael@0 105 TestSameSize();
michael@0 106 TestToBiggerSize();
michael@0 107 TestToSmallerSize();
michael@0 108 }

mercurial