File: copy_test.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 836 kB
  • sloc: makefile: 4
file content (89 lines) | stat: -rw-r--r-- 1,449 bytes parent folder | download
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
package missinggo

import (
	"bytes"
	"strings"
	"testing"
)

func TestCopyToArray(t *testing.T) {
	var arr [3]byte
	bb := []byte{1, 2, 3}
	CopyExact(&arr, bb)
	if !bytes.Equal(arr[:], bb) {
		t.FailNow()
	}
}

func TestCopyToSlicedArray(t *testing.T) {
	var arr [5]byte
	CopyExact(arr[:], "hello")
	if !bytes.Equal(arr[:], []byte("hello")) {
		t.FailNow()
	}
}

func TestCopyDestNotAddr(t *testing.T) {
	defer func() {
		r := recover()
		if r == nil {
			t.FailNow()
		}
		t.Log(r)
	}()
	var arr [3]byte
	CopyExact(arr, "nope")
}

func TestCopyLenMismatch(t *testing.T) {
	defer func() {
		r := recover()
		if r == nil {
			t.FailNow()
		}
		t.Log(r)
	}()
	CopyExact(make([]byte, 2), "abc")
}

func TestCopySrcString(t *testing.T) {
	dest := make([]byte, 3)
	CopyExact(dest, "lol")
	if string(dest) != "lol" {
		t.FailNow()
	}
	func() {
		defer func() {
			r := recover()
			if r == nil {
				t.FailNow()
			}
		}()
		CopyExact(dest, "rofl")
	}()
	var arr [5]byte
	CopyExact(&arr, interface{}("hello"))
	if string(arr[:]) != "hello" {
		t.FailNow()
	}
}

func TestCopySrcNilInterface(t *testing.T) {
	var arr [3]byte
	defer func() {
		r := recover().(string)
		if !strings.Contains(r, "invalid source") {
			t.FailNow()
		}
	}()
	CopyExact(&arr, nil)
}

func TestCopySrcPtr(t *testing.T) {
	var bigDst [1024]byte
	var bigSrc [1024]byte = [1024]byte{'h', 'i'}
	CopyExact(&bigDst, &bigSrc)
	if !bytes.Equal(bigDst[:], bigSrc[:]) {
		t.FailNow()
	}
}