Debian Codegraph @

internal/sort/map_private_test.go

raw
1 // Copyright (c) 2018-2025, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package sort
8
9 import (
10 "reflect"
11 "sort"
12 "testing"
13 )
14
15 func TestKvSlice(t *testing.T) {
16 t.Run("len=0", func(t *testing.T) {
17 kvs := newKvSlice(0)
18 if kvs.s != nil || kvs.v != nil {
19 t.Errorf("newKvSlice failed: %v", *kvs)
20 }
21 sort.Sort(kvs)
22 })
23
24 t.Run("len=1", func(t *testing.T) {
25 kvs := newKvSlice(1)
26 if kvs.s == nil || kvs.v != nil {
27 t.Errorf("newKvSlice failed: %v", *kvs)
28 }
29 kvs.s = append(kvs.s, kv{
30 key: reflect.ValueOf("a"),
31 value: reflect.ValueOf(1),
32 })
33 sort.Sort(kvs)
34 })
35
36 t.Run("len>1", func(t *testing.T) {
37 kvs := newKvSlice(3)
38 if kvs.s == nil || kvs.v == nil {
39 t.Errorf("newKvSlice failed: %v", *kvs)
40 }
41 kvs.s = append(kvs.s,
42 kv{
43 key: reflect.ValueOf("b"),
44 value: reflect.ValueOf(2),
45 },
46 kv{
47 key: reflect.ValueOf("c"),
48 value: reflect.ValueOf(3),
49 },
50 kv{
51 key: reflect.ValueOf("a"),
52 value: reflect.ValueOf(1),
53 },
54 )
55 sort.Sort(kvs)
56
57 if kvs.s[0].key.String() != "a" ||
58 kvs.s[1].key.String() != "b" ||
59 kvs.s[2].key.String() != "c" {
60 t.Errorf("Sort failed: [%v, %v, %v]",
61 kvs.s[0].key, kvs.s[1].key, kvs.s[2].key)
62 }
63 })
64 }