Compare commits

..

2 Commits

Author SHA1 Message Date
b26c64d0d9 Add more complex test case
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Has been cancelled
Run Tests / Test (push) Has been cancelled
2024-07-28 14:54:30 +02:00
930e5b9b4f Generate mask on the fly 2024-07-28 14:51:25 +02:00
2 changed files with 43 additions and 19 deletions

View File

@@ -7,17 +7,6 @@ import (
type GSM7Coder struct{}
var masks = []byte{
0b00000000,
0b00000001,
0b00000011,
0b00000111,
0b00001111,
0b00011111,
0b00111111,
0b01111111,
0b11111111,
}
func (c *GSM7Coder) Encode(s string, buf *bytes.Buffer) error {
// utf8 := *(*[]byte)(unsafe.Pointer(&s))
@@ -33,14 +22,14 @@ func (c *GSM7Coder) Encode(s string, buf *bytes.Buffer) error {
if bindex == 0 {
continue
}
mask := masks[bitshift]
mask := byte(255 >> (8 - bitshift))
masked := (mask & septet) << (8 - bitshift)
utf8[bindex-offset] |= masked
utf8[bindex] >>= bitshift
buf.WriteByte(utf8[bindex-offset])
bitshift++
if utf8[bindex] == 0 {
if bitshift == 8 {
offset++
bitshift = 1
}

View File

@@ -22,6 +22,40 @@ func TestGSM7EncodeSimpleASCIIString(t *testing.T) {
}
}
func TestGSM7EncodeComplexASCIIString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := "Golden rays play, Chasing night away."
expected := []byte{0b11000111, 0b00110111, 0b10011011, 0b01011100, 0b01110110, 0b10000011, 0b11100100, 0b11100001, 0b11111100, 0b00011100, 0b00000100, 0b01100111, 0b10000111, 0b11110011, 0b00101100, 0b11010000, 0b00010000, 0b00011101, 0b10011110, 0b10100111, 0b11011101, 0b01100111, 0b10010000, 0b00111011, 0b01111101, 0b01000110, 0b11010011, 0b01000001, 0b11100001, 0b01111011, 0b00111000, 0b11101111, 0b00000010}
err := coder.Encode(input, &buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if !bytes.Equal(buf.Bytes(), expected) {
t.Errorf("Expected %v, but got %v", expected, buf.Bytes())
}
}
func TestGSM7EncodeEmptyString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := ""
expected := []byte{}
err := coder.Encode(input, &buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if !bytes.Equal(buf.Bytes(), expected) {
t.Errorf("Expected %v, but got %v", expected, buf.Bytes())
}
}
func TestGSM7DecodeSimpleASCIIString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
@@ -40,20 +74,21 @@ func TestGSM7DecodeSimpleASCIIString(t *testing.T) {
}
}
func TestGSM7EncodeEmptyString(t *testing.T) {
func TestGSM7DecodeComplexASCIIString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := ""
input := []byte{0b11010011, 0b10111010, 0b01111011, 0b10001110, 0b01001110, 0b10111011, 0b11001011}
expected := []byte{}
err := coder.Encode(input, &buf)
expected := "Golden rays play, Chasing night away."
buf.Write(input)
output, err := coder.Decode(&buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if !bytes.Equal(buf.Bytes(), expected) {
t.Errorf("Expected %v, but got %v", expected, buf.Bytes())
if output != expected {
t.Errorf("Expected %v, but got %v", expected, output)
}
}