Compare commits

..

3 Commits

Author SHA1 Message Date
92c0e83e73 Update coders with error return values
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 25s
Run Tests / Test (push) Failing after 23s
2024-07-25 16:53:14 +02:00
0eb0f5d773 Add tests for gsm7 2024-07-25 16:33:46 +02:00
161ff14189 Add ascii encoding 2024-07-25 16:33:31 +02:00
7 changed files with 190 additions and 0 deletions

14
encoding/ascii.go Normal file
View File

@@ -0,0 +1,14 @@
package encoding
import "bytes"
type ASCIICoder struct{}
func (c *ASCIICoder) Encode(s string, buf *bytes.Buffer) error {
buf.WriteString(s)
return nil
}
func (c *ASCIICoder) Decode(buf *bytes.Buffer) (string, error) {
return buf.String(), nil
}

74
encoding/ascii_test.go Normal file
View File

@@ -0,0 +1,74 @@
package encoding
import (
"bytes"
"testing"
)
func TestASCIIEncodeSimpleASCIIString(t *testing.T) {
coder := &ASCIICoder{}
var buf bytes.Buffer
input := "Hello, World!"
expected := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}
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 TestASCIIDecodeSimpleASCIIString(t *testing.T) {
coder := &ASCIICoder{}
var buf bytes.Buffer
input := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}
expected := "Hello, World!"
buf.Write(input)
output, err := coder.Decode(&buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if output != expected {
t.Errorf("Expected %v, but got %v", expected, output)
}
}
func TestASCIIEncodeEmptyString(t *testing.T) {
coder := &ASCIICoder{}
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 TestASCIIDecodeEmptyString(t *testing.T) {
coder := &ASCIICoder{}
buf := bytes.NewBuffer([]byte{})
expected := ""
output, err := coder.Decode(buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if output != expected {
t.Errorf("Expected %v, but got %v", expected, output)
}
}

5
encoding/charset.json Normal file
View File

@@ -0,0 +1,5 @@
{
"ucs2": {
"link": "http://www.columbia.edu/kermit/ucs2.html"
}
}

8
encoding/coder.go Normal file
View File

@@ -0,0 +1,8 @@
package encoding
import "bytes"
type Coder interface {
Encode(s string, buf *bytes.Buffer) error
Decode(buf *bytes.Buffer) (string, error)
}

18
encoding/gsm7.go Normal file
View File

@@ -0,0 +1,18 @@
package encoding
import (
"bytes"
"log"
)
type GSM7Coder struct{}
func (c *GSM7Coder) Encode(s string, buf *bytes.Buffer) {
utf8 := []byte(s)
log.Println(utf8)
buf.Write(utf8)
}
func (c *GSM7Coder) Decode(buf *bytes.Buffer) string {
return buf.String()
}

58
encoding/gsm7_test.go Normal file
View File

@@ -0,0 +1,58 @@
package encoding
import (
"bytes"
"testing"
)
func TestGSM7EncodeSimpleASCIIString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := "Sunshine"
expected := []byte{0b11010011, 0b10111001, 0b10111011, 0b10001110, 0b01001110, 0b10111011, 0b11001011}
coder.Encode(input, &buf)
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
input := []byte{0b11010011, 0b10111001, 0b10111011, 0b10001110, 0b01001110, 0b10111011, 0b11001011}
expected := "Sunshine"
buf.Write(input)
output := coder.Decode(&buf)
if output != expected {
t.Errorf("Expected %v, but got %v", expected, output)
}
}
func TestGSM7EncodeEmptyString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := ""
expected := []byte{}
coder.Encode(input, &buf)
if !bytes.Equal(buf.Bytes(), expected) {
t.Errorf("Expected %v, but got %v", expected, buf.Bytes())
}
}
func TestGSM7DecodeEmptyString(t *testing.T) {
coder := &GSM7Coder{}
buf := bytes.NewBuffer([]byte{})
expected := ""
output := coder.Decode(buf)
if output != expected {
t.Errorf("Expected %v, but got %v", expected, output)
}
}

13
encoding/ucs2.go Normal file
View File

@@ -0,0 +1,13 @@
package encoding
import "bytes"
type UCS2Coder struct{}
func (c *UCS2Coder) Encode(s string, buf *bytes.Buffer) {
panic("UCS2 not implemented yet")
}
func (c *UCS2Coder) Decode(buf *bytes.Buffer) string {
panic("UCS2 not implemented yet")
}