-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv_test.go
95 lines (82 loc) · 2.08 KB
/
conv_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* This file is part of the N2CMU (https://github.com/nthnn/n2cmu).
* Copyright (c) 2024 Nathanne Isip.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"testing"
"github.com/nthnn/n2cu/util"
)
func TestFloat32ToBytesAndBack(t *testing.T) {
tests := []struct {
input float32
}{
{input: 3.14},
{input: -123.456},
{input: 0.0},
}
for _, test := range tests {
bytes := util.Float32ToBytes(test.input)
result := util.BytesToFloat32(bytes)
if result != test.input {
t.Errorf("Expected %f, but got %f", test.input, result)
} else {
fmt.Printf("%v = %.3f\r\n", bytes, result)
}
}
}
func TestUint16ToBytesAndBack(t *testing.T) {
tests := []struct {
input uint16
}{
{input: 100},
{input: 65535},
{input: 0},
}
for _, test := range tests {
bytes := util.Uint16ToBytes(test.input)
result := util.BytesToUint16(bytes)
if result != test.input {
t.Errorf("Expected %d, but got %d", test.input, result)
} else {
fmt.Printf("%v = %d\r\n", bytes, result)
}
}
}
func BenchmarkFloat32ToBytes(b *testing.B) {
input := float32(3.14)
for i := 0; i < b.N; i++ {
_ = util.Float32ToBytes(input)
}
}
func BenchmarkBytesToFloat32(b *testing.B) {
input := [4]byte{1, 2, 3, 4}
for i := 0; i < b.N; i++ {
_ = util.BytesToFloat32(input)
}
}
func BenchmarkUint16ToBytes(b *testing.B) {
input := uint16(100)
for i := 0; i < b.N; i++ {
_ = util.Uint16ToBytes(input)
}
}
func BenchmarkBytesToUint16(b *testing.B) {
input := [2]byte{1, 2}
for i := 0; i < b.N; i++ {
_ = util.BytesToUint16(input)
}
}