File: auxiliary_test.go

package info (click to toggle)
golang-github-awnumar-memguard 0.22.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 572 kB
  • sloc: makefile: 3
file content (53 lines) | stat: -rw-r--r-- 1,255 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
package core

import (
	"bytes"
	"fmt"
	"testing"
	"unsafe"
)

func TestRoundToPageSize(t *testing.T) {
	fmt.Println("System page size:", pageSize, "bytes")

	if roundToPageSize(0) != 0 {
		t.Error("failed with test input 0")
	}
	if roundToPageSize(1) != pageSize {
		t.Error("failed with test input 1")
	}
	if roundToPageSize(pageSize) != pageSize {
		t.Error("failed with test input page_size")
	}
	if roundToPageSize(pageSize+1) != 2*pageSize {
		t.Error("failed with test input page_size + 1")
	}
}

func TestGetBytes(t *testing.T) {
	// Allocate an ordinary buffer.
	buffer := make([]byte, 32)

	// Get am alternate reference to it using our slice builder.
	derived := getBytes(&buffer[0], len(buffer))

	// Check for naive equality.
	if !bytes.Equal(buffer, derived) {
		t.Error("naive equality check failed")
	}

	// Modify and check if the change was reflected in both.
	buffer[0] = 1
	buffer[31] = 1
	if !bytes.Equal(buffer, derived) {
		t.Error("modified equality check failed")
	}

	// Do a deep comparison.
	if uintptr(unsafe.Pointer(&buffer[0])) != uintptr(unsafe.Pointer(&derived[0])) {
		t.Error("pointer values differ")
	}
	if len(buffer) != len(derived) || cap(buffer) != cap(derived) {
		t.Error("length or capacity values differ")
	}
}