summaryrefslogtreecommitdiff
path: root/lib/btrfs/btrfsprim/key_test.go
blob: 6274b43f18ba47fa2afa8f50afd02754d8d1f094 (plain)
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
// Copyright (C) 2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package btrfsprim

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func k(objID ObjID, typ ItemType, offset uint64) Key {
	return Key{
		ObjectID: objID,
		ItemType: typ,
		Offset:   offset,
	}
}

func eq(t *testing.T, act, exp Key) {
	t.Helper()
	assert.Equal(t, exp, act)
}

func ppEq(t *testing.T, in, exp Key) {
	t.Helper()
	eq(t, in.Pp(), exp)
	if in != MaxKey {
		eq(t, exp.Mm(), in)
	}
}

func mmEq(t *testing.T, in, exp Key) {
	t.Helper()
	eq(t, in.Mm(), exp)
	if in != (Key{}) {
		eq(t, exp.Pp(), in)
	}
}

func TestKey(t *testing.T) {
	t.Parallel()

	eq(t, MaxKey, k(18446744073709551615, 255, 18446744073709551615))

	// pp
	ppEq(t, k(0, 0, 0), k(0, 0, 1))
	ppEq(t, k(0, 0, 18446744073709551615), k(0, 1, 0))
	ppEq(t, k(0, 255, 0), k(0, 255, 1))
	ppEq(t, k(0, 255, 18446744073709551615), k(1, 0, 0))
	ppEq(t, MaxKey, k(18446744073709551615, 255, 18446744073709551615))

	// mm
	mmEq(t, MaxKey, k(18446744073709551615, 255, 18446744073709551614))
	mmEq(t, k(18446744073709551615, 255, 0), k(18446744073709551615, 254, 18446744073709551615))
	mmEq(t, k(18446744073709551615, 0, 0), k(18446744073709551614, 255, 18446744073709551615))
	mmEq(t, k(0, 0, 0), k(0, 0, 0))
}