| 1 | package gocache |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestEntry_SizeInBytes(t *testing.T) { |
| 9 | testSizeInBytes(t, "key", 0, 75) |
| 10 | testSizeInBytes(t, "k", 0, 73) |
| 11 | testSizeInBytes(t, "k", "v", 66) |
| 12 | testSizeInBytes(t, "k", true, 66) |
| 13 | testSizeInBytes(t, "k", int8(1), 66) |
| 14 | testSizeInBytes(t, "k", uint8(1), 66) |
| 15 | testSizeInBytes(t, "k", true, 66) |
| 16 | testSizeInBytes(t, "k", int16(1), 67) |
| 17 | testSizeInBytes(t, "k", uint16(1), 67) |
| 18 | testSizeInBytes(t, "k", int32(1), 69) |
| 19 | testSizeInBytes(t, "k", uint32(1), 69) |
| 20 | testSizeInBytes(t, "k", float32(1), 69) |
| 21 | testSizeInBytes(t, "k", complex64(1), 69) |
| 22 | testSizeInBytes(t, "k", int64(1), 73) |
| 23 | testSizeInBytes(t, "k", uint64(1), 73) |
| 24 | testSizeInBytes(t, "k", 1, 73) |
| 25 | testSizeInBytes(t, "k", uint(1), 73) |
| 26 | testSizeInBytes(t, "k", float64(1), 73) |
| 27 | testSizeInBytes(t, "k", complex128(1), 73) |
| 28 | testSizeInBytes(t, "k", []string{}, 65) |
| 29 | testSizeInBytes(t, "k", []string{"what"}, 85) |
| 30 | testSizeInBytes(t, "k", []string{"what", "the"}, 104) |
| 31 | testSizeInBytes(t, "k", []int8{}, 65) |
| 32 | testSizeInBytes(t, "k", []int8{1}, 66) |
| 33 | testSizeInBytes(t, "k", []int8{1, 2}, 67) |
| 34 | testSizeInBytes(t, "k", []uint8{1}, 66) |
| 35 | testSizeInBytes(t, "k", []uint8{1, 2}, 67) |
| 36 | testSizeInBytes(t, "k", []bool{true}, 66) |
| 37 | testSizeInBytes(t, "k", []bool{true, false}, 67) |
| 38 | testSizeInBytes(t, "k", []int16{1}, 67) |
| 39 | testSizeInBytes(t, "k", []int16{1, 2}, 69) |
| 40 | testSizeInBytes(t, "k", []uint16{1}, 67) |
| 41 | testSizeInBytes(t, "k", []int32{1}, 69) |
| 42 | testSizeInBytes(t, "k", []int32{1, 2}, 73) |
| 43 | testSizeInBytes(t, "k", []uint32{1}, 69) |
| 44 | testSizeInBytes(t, "k", []uint32{1, 2}, 73) |
| 45 | testSizeInBytes(t, "k", []float32{1}, 69) |
| 46 | testSizeInBytes(t, "k", []float32{1, 2}, 73) |
| 47 | testSizeInBytes(t, "k", []complex64{1}, 69) |
| 48 | testSizeInBytes(t, "k", []complex64{1, 2}, 73) |
| 49 | testSizeInBytes(t, "k", []int64{1}, 73) |
| 50 | testSizeInBytes(t, "k", []int64{1, 2}, 81) |
| 51 | testSizeInBytes(t, "k", []uint64{1}, 73) |
| 52 | testSizeInBytes(t, "k", []uint64{1, 2}, 81) |
| 53 | testSizeInBytes(t, "k", []int{1}, 73) |
| 54 | testSizeInBytes(t, "k", []int{1, 2}, 81) |
| 55 | testSizeInBytes(t, "k", []uint{1}, 73) |
| 56 | testSizeInBytes(t, "k", []uint{1, 2}, 81) |
| 57 | testSizeInBytes(t, "k", []float64{1}, 73) |
| 58 | testSizeInBytes(t, "k", []float64{1, 2}, 81) |
| 59 | testSizeInBytes(t, "k", []complex128{1}, 73) |
| 60 | testSizeInBytes(t, "k", []complex128{1, 2}, 81) |
| 61 | testSizeInBytes(t, "k", struct{}{}, 67) |
| 62 | testSizeInBytes(t, "k", struct{ A string }{A: "hello"}, 72) |
| 63 | testSizeInBytes(t, "k", struct{ A, B string }{A: "hello", B: "world"}, 78) |
| 64 | testSizeInBytes(t, "k", nil, 70) |
| 65 | testSizeInBytes(t, "k", make([]any, 5), 170) |
| 66 | } |
| 67 | |
| 68 | func testSizeInBytes(t *testing.T, key string, value any, expectedSize int) { |
| 69 | t.Run(fmt.Sprintf("%T_%d", value, expectedSize), func(t *testing.T) { |
| 70 | if size := (&Entry{Key: key, Value: value}).SizeInBytes(); size != expectedSize { |
| 71 | t.Errorf("expected size of entry with key '%v' and value '%v' (%T) to be %d, got %d", key, value, value, expectedSize, size) |
| 72 | } |
| 73 | }) |
| 74 | } |