File: hash_test.go

package info (click to toggle)
golang-github-mna-redisc 1.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 308 kB
  • sloc: ruby: 1,261; sh: 101; makefile: 5
file content (75 lines) | stat: -rw-r--r-- 1,358 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
package redisc

import (
	"strings"
	"testing"

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

func TestSlot(t *testing.T) {
	cases := []struct {
		in  string
		out int
	}{
		{"", 0},
		{"a", 15495},
		{"b", 3300},
		{"ab", 13567},
		{"abc", 7638},
		{"a{b}", 3300},
		{"{a}b", 15495},
		{"{a}{b}", 15495},
		{"{}{a}{b}", 11267},
		{"a{b}c", 3300},
		{"{a}bc", 15495},
		{"{a}{b}{c}", 15495},
		{"{}{a}{b}{c}", 1044},
		{"a{bc}d", 12685},
		{"a{bcd}", 1872},
		{"{abcd}", 10294},
		{"abcd", 10294},
		{"{a", 10276},
		{"a}", 5921},
		{"123456789", 12739},
		{"a≠b", 11870},
		{"•", 97},
		{"a{}{b}c", 14872},
	}

	for _, c := range cases {
		got := Slot(c.in)
		assert.Equal(t, c.out, got, c.in)
	}
}

func TestSplitBySlot(t *testing.T) {
	cases := []struct {
		// join/split by comma, for convenience
		in  string
		out []string
	}{
		{"", nil},
		{"a", []string{"a"}},
		{"a,b", []string{"b", "a"}},
		{"a,b,cb{a}", []string{"b", "a,cb{a}"}},
		{"a,b,cb{a},a{b}", []string{"b,a{b}", "a,cb{a}"}},
		{"a,b,cb{a},a{b},abc", []string{"b,a{b}", "abc", "a,cb{a}"}},
	}

	for _, c := range cases {
		args := strings.Split(c.in, ",")
		if c.in == "" {
			args = nil
		}
		got := SplitBySlot(args...)

		exp := make([][]string, len(c.out))
		for i, o := range c.out {
			exp[i] = strings.Split(o, ",")
		}

		assert.Equal(t, exp, got, c.in)
		t.Logf("%#v", got)
	}
}