File: string_cache_test.go

package info (click to toggle)
golang-github-oschwald-maxminddb-golang-v2 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: perl: 557; makefile: 3
file content (51 lines) | stat: -rw-r--r-- 1,238 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
package decoder

import (
	"testing"

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

func TestStringCacheOffsetZero(t *testing.T) {
	cache := newStringCache()
	data := []byte("hello world, this is test data")

	// Test string at offset 0
	str1 := cache.internAt(0, 5, data)
	require.Equal(t, "hello", str1)

	// Second call should hit cache and return same interned string
	str2 := cache.internAt(0, 5, data)
	require.Equal(t, "hello", str2)

	// Note: Both strings should be identical (cache hit)
	// We can't easily test if they're the same object without unsafe,
	// but correctness is verified by the equal values
}

func TestStringCacheVariousOffsets(t *testing.T) {
	cache := newStringCache()
	data := []byte("abcdefghijklmnopqrstuvwxyz")

	testCases := []struct {
		offset   uint
		size     uint
		expected string
	}{
		{0, 3, "abc"},
		{5, 3, "fgh"},
		{10, 5, "klmno"},
		{23, 3, "xyz"},
	}

	for _, tc := range testCases {
		// First call
		str1 := cache.internAt(tc.offset, tc.size, data)
		require.Equal(t, tc.expected, str1)

		// Second call should hit cache
		str2 := cache.internAt(tc.offset, tc.size, data)
		require.Equal(t, tc.expected, str2)
		// Verify cache hit returns correct value (interning tested via behavior)
	}
}