Tanoda
BitConverterNonAllocTests.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Linq;
11using NUnit.Framework;
12
14
15 public class BitConverterTests {
16
17 private byte[] _bytes;
18
19 [SetUp]
20 public void SetUp() {
21 _bytes = new byte[128];
22 for (int i = 0; i < _bytes.Length; i++) {
23 _bytes[i] = (byte)UnityEngine.Random.Range(int.MinValue, int.MaxValue);
24 }
25 }
26
27 [Test]
28 public void TestToUInt16() {
29 UInt16 expected = BitConverter.ToUInt16(_bytes, 0);
30 UInt16 actual = BitConverterNonAlloc.ToUInt16(_bytes, 0);
31
32 Assert.That(actual, Is.EqualTo(expected));
33 }
34
35 [Test]
36 public void TestFromUInt16() {
37 UInt16 value = (UInt16)UnityEngine.Random.Range(float.MinValue, float.MaxValue);
38 var actual = BitConverter.GetBytes(value);
39
40 int offset = 0;
41 BitConverterNonAlloc.GetBytes(value, _bytes, ref offset);
42
43 Assert.That(offset, Is.EqualTo(actual.Length));
44 Assert.That(_bytes.Take(offset), Is.EquivalentTo(actual));
45 }
46
47 [Test]
48 public void TestToInt16() {
49 Int16 expected = BitConverter.ToInt16(_bytes, 0);
50 Int16 actual = BitConverterNonAlloc.ToInt16(_bytes, 0);
51
52 Assert.That(actual, Is.EqualTo(expected));
53 }
54
55 [Test]
56 public void TestFromInt16() {
57 Int16 value = (Int16)UnityEngine.Random.Range(float.MinValue, float.MaxValue);
58 var actual = BitConverter.GetBytes(value);
59
60 int offset = 0;
61 BitConverterNonAlloc.GetBytes(value, _bytes, ref offset);
62
63 Assert.That(offset, Is.EqualTo(actual.Length));
64 Assert.That(_bytes.Take(offset), Is.EquivalentTo(actual));
65 }
66
67 [Test]
68 public void TestToUInt32() {
69 UInt32 expected = BitConverter.ToUInt32(_bytes, 0);
70 UInt32 actual = BitConverterNonAlloc.ToUInt32(_bytes, 0);
71
72 Assert.That(actual, Is.EqualTo(expected));
73 }
74
75 [Test]
76 public void TestFromUInt32() {
77 UInt32 value = (UInt32)UnityEngine.Random.Range(float.MinValue, float.MaxValue);
78 var actual = BitConverter.GetBytes(value);
79
80 int offset = 0;
81 BitConverterNonAlloc.GetBytes(value, _bytes, ref offset);
82
83 Assert.That(offset, Is.EqualTo(actual.Length));
84 Assert.That(_bytes.Take(offset), Is.EquivalentTo(actual));
85 }
86
87 [Test]
88 public void TestToInt32() {
89 Int32 expected = BitConverter.ToInt32(_bytes, 0);
90 Int32 actual = BitConverterNonAlloc.ToInt32(_bytes, 0);
91
92 Assert.That(actual, Is.EqualTo(expected));
93 }
94
95 [Test]
96 public void TestFromInt32() {
97 Int32 value = (Int32)UnityEngine.Random.Range(float.MinValue, float.MaxValue);
98 var actual = BitConverter.GetBytes(value);
99
100 int offset = 0;
101 BitConverterNonAlloc.GetBytes(value, _bytes, ref offset);
102
103 Assert.That(offset, Is.EqualTo(actual.Length));
104 Assert.That(_bytes.Take(offset), Is.EquivalentTo(actual));
105 }
106
107 [Test]
108 public void TestToUInt64() {
109 UInt64 expected = BitConverter.ToUInt64(_bytes, 0);
110 UInt64 actual = BitConverterNonAlloc.ToUInt64(_bytes, 0);
111
112 Assert.That(actual, Is.EqualTo(expected));
113 }
114
115 [Test]
116 public void TestFromUInt64() {
117 UInt64 value = (UInt64)UnityEngine.Random.Range(float.MinValue, float.MaxValue);
118 var actual = BitConverter.GetBytes(value);
119
120 int offset = 0;
121 BitConverterNonAlloc.GetBytes(value, _bytes, ref offset);
122
123 Assert.That(offset, Is.EqualTo(actual.Length));
124 Assert.That(_bytes.Take(offset), Is.EquivalentTo(actual));
125 }
126
127 [Test]
128 public void TestToInt64() {
129 Int64 expected = BitConverter.ToInt64(_bytes, 0);
130 Int64 actual = BitConverterNonAlloc.ToInt64(_bytes, 0);
131
132 Assert.That(actual, Is.EqualTo(expected));
133 }
134
135 [Test]
136 public void TestFromInt64() {
137 Int64 value = (Int64)UnityEngine.Random.Range(float.MinValue, float.MaxValue);
138 var actual = BitConverter.GetBytes(value);
139
140 int offset = 0;
141 BitConverterNonAlloc.GetBytes(value, _bytes, ref offset);
142
143 Assert.That(offset, Is.EqualTo(actual.Length));
144 Assert.That(_bytes.Take(offset), Is.EquivalentTo(actual));
145 }
146
147 [Test]
148 public void TestToSingle() {
149 Single expected = BitConverter.ToSingle(_bytes, 0);
150 Single actual = BitConverterNonAlloc.ToSingle(_bytes, 0);
151
152 Assert.That(actual, Is.EqualTo(expected));
153 }
154
155 [Test]
156 public void TestFromSingle() {
157 Single value = (Single)UnityEngine.Random.Range(float.MinValue, float.MaxValue);
158 var actual = BitConverter.GetBytes(value);
159
160 int offset = 0;
161 BitConverterNonAlloc.GetBytes(value, _bytes, ref offset);
162
163 Assert.That(offset, Is.EqualTo(actual.Length));
164 Assert.That(_bytes.Take(offset), Is.EquivalentTo(actual));
165 }
166
167 [Test]
168 public void TestToDouble() {
169 Double expected = BitConverter.ToDouble(_bytes, 0);
170 Double actual = BitConverterNonAlloc.ToDouble(_bytes, 0);
171
172 Assert.That(actual, Is.EqualTo(expected));
173 }
174
175 [Test]
176 public void TestFromDouble() {
177 Double value = (Double)UnityEngine.Random.Range(float.MinValue, float.MaxValue);
178 var actual = BitConverter.GetBytes(value);
179
180 int offset = 0;
181 BitConverterNonAlloc.GetBytes(value, _bytes, ref offset);
182
183 Assert.That(offset, Is.EqualTo(actual.Length));
184 Assert.That(_bytes.Take(offset), Is.EquivalentTo(actual));
185 }
186 }
187}