MCPcopy Create free account
hub / github.com/apache/fory / TestEncodeAndDecodeMetaString

Function TestEncodeAndDecodeMetaString

go/fory/meta/meta_string_test.go:26–72  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

24)
25
26func TestEncodeAndDecodeMetaString(t *testing.T) {
27 var data MetaString
28 var dst string
29 var err error
30
31 str2bits := map[string]int{
32 // "abc_def" should be encoded as 0|00000|00, 001|00010|, 11011|000, 11|00100|0, 0101
33 "abc_def": 5,
34 "org.apache.fory.benchmark.data": 5,
35 "HelloWorld__123.2024": 6,
36 "MediaContent": 5,
37 "Apple_banana": 5,
38 "你好,世界": 0, // not used
39 }
40 str2encoding := map[string]Encoding{
41 "abc_def": LOWER_SPECIAL,
42 "org.apache.fory.benchmark.data": LOWER_SPECIAL,
43 "MediaContent": ALL_TO_LOWER_SPECIAL,
44 "HelloWorld__123.2024": LOWER_UPPER_DIGIT_SPECIAL,
45 "Apple_banana": FIRST_TO_LOWER_SPECIAL,
46 "你好,世界": UTF_8,
47 }
48 encoder := NewEncoder('.', '_')
49 decoder := NewDecoder('.', '_')
50
51 for src, bitsPerChar := range str2bits {
52 data, err = encoder.Encode(src)
53 require.Equal(t, nil, err)
54 require.Equal(t, str2encoding[src], data.GetEncoding())
55 require.Equal(t, calcTotalBytes(src, bitsPerChar, data.GetEncoding()), len(data.GetEncodedBytes()))
56 dst, err = decoder.Decode(data.GetEncodedBytes(), data.GetEncoding())
57 require.Equal(t, nil, err)
58 require.Equal(t, src, dst)
59 }
60
61 // error situation
62 dst, err = decoder.Decode([]byte{0xFF, 0x31}, LOWER_SPECIAL)
63 require.NotEqual(t, nil, err)
64
65 // empty string
66 data, err = encoder.Encode("")
67 require.Equal(t, nil, err)
68 require.Equal(t, 0, len(data.GetEncodedBytes()))
69 dst, err = decoder.Decode(data.GetEncodedBytes(), data.GetEncoding())
70 require.Equal(t, nil, err)
71 require.Equal(t, "", dst)
72}
73
74func calcTotalBytes(src string, bitsPerChar int, encoding Encoding) int {
75 if encoding == UTF_8 {

Callers

nothing calls this directly

Calls 8

EncodeMethod · 0.95
GetEncodingMethod · 0.95
GetEncodedBytesMethod · 0.95
DecodeMethod · 0.95
NewEncoderFunction · 0.85
NewDecoderFunction · 0.85
calcTotalBytesFunction · 0.85
EqualMethod · 0.45

Tested by

no test coverage detected