1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/Casting.h"
11 using mozilla::BitwiseCast
;
12 using mozilla::detail::IsInBounds
;
14 template <typename Uint
, typename Ulong
, bool = (sizeof(Uint
) == sizeof(Ulong
))>
15 struct UintUlongBitwiseCast
;
17 template <typename Uint
, typename Ulong
>
18 struct UintUlongBitwiseCast
<Uint
, Ulong
, true> {
20 MOZ_RELEASE_ASSERT(BitwiseCast
<Ulong
>(Uint(8675309)) == Ulong(8675309));
24 template <typename Uint
, typename Ulong
>
25 struct UintUlongBitwiseCast
<Uint
, Ulong
, false> {
29 static void TestBitwiseCast() {
30 MOZ_RELEASE_ASSERT(BitwiseCast
<int>(int(8675309)) == int(8675309));
31 UintUlongBitwiseCast
<unsigned int, unsigned long>::test();
34 static void TestSameSize() {
35 MOZ_RELEASE_ASSERT((IsInBounds
<int16_t, int16_t>(int16_t(0))));
36 MOZ_RELEASE_ASSERT((IsInBounds
<int16_t, int16_t>(int16_t(INT16_MIN
))));
37 MOZ_RELEASE_ASSERT((IsInBounds
<int16_t, int16_t>(int16_t(INT16_MAX
))));
38 MOZ_RELEASE_ASSERT((IsInBounds
<uint16_t, uint16_t>(uint16_t(UINT16_MAX
))));
39 MOZ_RELEASE_ASSERT((IsInBounds
<uint16_t, int16_t>(uint16_t(0))));
40 MOZ_RELEASE_ASSERT((!IsInBounds
<uint16_t, int16_t>(uint16_t(-1))));
41 MOZ_RELEASE_ASSERT((!IsInBounds
<int16_t, uint16_t>(int16_t(-1))));
42 MOZ_RELEASE_ASSERT((IsInBounds
<int16_t, uint16_t>(int16_t(INT16_MAX
))));
43 MOZ_RELEASE_ASSERT((!IsInBounds
<int16_t, uint16_t>(int16_t(INT16_MIN
))));
44 MOZ_RELEASE_ASSERT((IsInBounds
<int32_t, uint32_t>(int32_t(INT32_MAX
))));
45 MOZ_RELEASE_ASSERT((!IsInBounds
<int32_t, uint32_t>(int32_t(INT32_MIN
))));
48 static void TestToBiggerSize() {
49 MOZ_RELEASE_ASSERT((IsInBounds
<int16_t, int32_t>(int16_t(0))));
50 MOZ_RELEASE_ASSERT((IsInBounds
<int16_t, int32_t>(int16_t(INT16_MIN
))));
51 MOZ_RELEASE_ASSERT((IsInBounds
<int16_t, int32_t>(int16_t(INT16_MAX
))));
52 MOZ_RELEASE_ASSERT((IsInBounds
<uint16_t, uint32_t>(uint16_t(UINT16_MAX
))));
53 MOZ_RELEASE_ASSERT((IsInBounds
<uint16_t, int32_t>(uint16_t(0))));
54 MOZ_RELEASE_ASSERT((IsInBounds
<uint16_t, int32_t>(uint16_t(-1))));
55 MOZ_RELEASE_ASSERT((!IsInBounds
<int16_t, uint32_t>(int16_t(-1))));
56 MOZ_RELEASE_ASSERT((IsInBounds
<int16_t, uint32_t>(int16_t(INT16_MAX
))));
57 MOZ_RELEASE_ASSERT((!IsInBounds
<int16_t, uint32_t>(int16_t(INT16_MIN
))));
58 MOZ_RELEASE_ASSERT((IsInBounds
<int32_t, uint64_t>(int32_t(INT32_MAX
))));
59 MOZ_RELEASE_ASSERT((!IsInBounds
<int32_t, uint64_t>(int32_t(INT32_MIN
))));
62 static void TestToSmallerSize() {
63 MOZ_RELEASE_ASSERT((IsInBounds
<int16_t, int8_t>(int16_t(0))));
64 MOZ_RELEASE_ASSERT((!IsInBounds
<int16_t, int8_t>(int16_t(INT16_MIN
))));
65 MOZ_RELEASE_ASSERT((!IsInBounds
<int16_t, int8_t>(int16_t(INT16_MAX
))));
66 MOZ_RELEASE_ASSERT((!IsInBounds
<uint16_t, uint8_t>(uint16_t(UINT16_MAX
))));
67 MOZ_RELEASE_ASSERT((IsInBounds
<uint16_t, int8_t>(uint16_t(0))));
68 MOZ_RELEASE_ASSERT((!IsInBounds
<uint16_t, int8_t>(uint16_t(-1))));
69 MOZ_RELEASE_ASSERT((!IsInBounds
<int16_t, uint8_t>(int16_t(-1))));
70 MOZ_RELEASE_ASSERT((!IsInBounds
<int16_t, uint8_t>(int16_t(INT16_MAX
))));
71 MOZ_RELEASE_ASSERT((!IsInBounds
<int16_t, uint8_t>(int16_t(INT16_MIN
))));
72 MOZ_RELEASE_ASSERT((!IsInBounds
<int32_t, uint16_t>(int32_t(INT32_MAX
))));
73 MOZ_RELEASE_ASSERT((!IsInBounds
<int32_t, uint16_t>(int32_t(INT32_MIN
))));
76 MOZ_RELEASE_ASSERT((!IsInBounds
<int64_t, int32_t>(int64_t(INT32_MIN
) - 1)));
77 MOZ_RELEASE_ASSERT((IsInBounds
<int64_t, int32_t>(int64_t(INT32_MIN
))));
78 MOZ_RELEASE_ASSERT((IsInBounds
<int64_t, int32_t>(int64_t(INT32_MIN
) + 1)));
79 MOZ_RELEASE_ASSERT((IsInBounds
<int64_t, int32_t>(int64_t(INT32_MAX
) - 1)));
80 MOZ_RELEASE_ASSERT((IsInBounds
<int64_t, int32_t>(int64_t(INT32_MAX
))));
81 MOZ_RELEASE_ASSERT((!IsInBounds
<int64_t, int32_t>(int64_t(INT32_MAX
) + 1)));
83 MOZ_RELEASE_ASSERT((!IsInBounds
<int64_t, uint32_t>(int64_t(-1))));
84 MOZ_RELEASE_ASSERT((IsInBounds
<int64_t, uint32_t>(int64_t(0))));
85 MOZ_RELEASE_ASSERT((IsInBounds
<int64_t, uint32_t>(int64_t(1))));
86 MOZ_RELEASE_ASSERT((IsInBounds
<int64_t, uint32_t>(int64_t(UINT32_MAX
) - 1)));
87 MOZ_RELEASE_ASSERT((IsInBounds
<int64_t, uint32_t>(int64_t(UINT32_MAX
))));
88 MOZ_RELEASE_ASSERT((!IsInBounds
<int64_t, uint32_t>(int64_t(UINT32_MAX
) + 1)));