Skip to content

Commit

Permalink
chore: test cases (#28)
Browse files Browse the repository at this point in the history
1. TestBitBuffer
2. TestReedSolomonMultiply
3. TestNumCharCountBits
  • Loading branch information
piglig committed Jul 18, 2023
1 parent 7833776 commit b32698f
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 2 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ jobs:
# run: go build -v ./...

- name: Test
run: go test -v -race ./...
run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
- name: Vet
run: go vet ./...
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

# - uses: dominikh/staticcheck-action@v1.2.0
# with:
# version: "2022.1.1"
52 changes: 52 additions & 0 deletions bit_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,55 @@ func checkEqual(b *BitBuffer, expected []bool) bool {
}
return true
}

func TestBitBuffer(t *testing.T) {
buffer := BitBuffer{}

// Testing len()
if buffer.len() != 0 {
t.Errorf("Expected length to be 0 but got %d", buffer.len())
}

// Testing set and getBit
buffer.set(2, true)
if !buffer.getBit(2) {
t.Errorf("Expected bit at position 2 to be true but got false")
}
if buffer.getBit(3) {
t.Errorf("Expected bit at position 3 to be false but got true")
}

// Testing grow
buffer.grow(10)
if buffer.len() != 10 {
t.Errorf("Expected length to be 10 but got %d", buffer.len())
}

// Testing appendBits
err := buffer.appendBits(15, 4)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}
if !buffer.getBit(10) || !buffer.getBit(11) || !buffer.getBit(12) || !buffer.getBit(13) {
t.Errorf("Expected bits 10-13 to be true but got false")
}

// Testing clone
cloneBuffer := buffer.clone()
for i := 0; i < buffer.len(); i++ {
if cloneBuffer.getBit(i) != buffer.getBit(i) {
t.Errorf("Expected bit at position %d to be equal in both buffers", i)
}
}

// Testing appendData
otherBuffer := BitBuffer{}
otherBuffer.set(0, true)
err = buffer.appendData(&otherBuffer)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}
if !buffer.getBit(buffer.len() - 1) {
t.Errorf("Expected last bit to be true but got false")
}
}
Empty file added go.sum
Empty file.
25 changes: 25 additions & 0 deletions qr_code_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package go_qr

import "testing"

func TestReedSolomonMultiply(t *testing.T) {
testCases := []struct {
x int
y int
expected int
}{
{0, 0, 0},
{1, 1, 1},
{2, 3, 6},
{4, 4, 16},
{5, 8, 40},
{9, 7, 63},
}

for _, tc := range testCases {
result := reedSolomonMultiply(tc.x, tc.y)
if result != tc.expected {
t.Errorf("For x: %d, y: %d, Expected: %d, but got: %d", tc.x, tc.y, tc.expected, result)
}
}
}
136 changes: 135 additions & 1 deletion qr_segment_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package go_qr

import "testing"
import (
"testing"
)

func TestMakeNumeric(t *testing.T) {
cases := []struct {
Expand Down Expand Up @@ -54,3 +56,135 @@ func TestIsNumeric(t *testing.T) {
}
}
}

func TestNumCharCountBits(t *testing.T) {
tests := []struct {
name string
mode Mode
ver int
want int
}{
{
"Numeric mode, version 0",
Numeric,
0,
10,
},
{
"Alphanumeric mode, version 20",
Alphanumeric,
20,
11,
},
{
"Byte mode, version 25",
Byte,
25,
16,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.mode.numCharCountBits(tt.ver); got != tt.want {
t.Errorf("Mode.numCharCountBits() = %v, want %v", got, tt.want)
}
})
}
}

func TestGetModeBits(t *testing.T) {
tests := []struct {
name string
mode Mode
want int
}{
{
"Numeric mode bits",
Numeric,
0x1,
},
{
"Alphanumeric mode bits",
Alphanumeric,
0x2,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.mode.getModeBits(); got != tt.want {
t.Errorf("Mode.getModeBits() = %v, want %v", got, tt.want)
}
})
}
}

func TestIsModes(t *testing.T) {
tests := []struct {
name string
mode Mode
isNumeric bool
isAlpha bool
isByte bool
isKanji bool
isEci bool
}{
{
"Numeric mode",
Numeric,
true,
false,
false,
false,
false,
},
{
"Alphanumeric mode",
Alphanumeric,
false,
true,
false,
false,
false,
},
{
"Byte mode",
Byte,
false,
false,
true,
false,
false,
},
{
"Eci mode",
Eci,
false,
false,
false,
false,
true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.mode.isNumeric(); got != tt.isNumeric {
t.Errorf("Mode.isNumeric() = %v, want %v", got, tt.isNumeric)
}
if got := tt.mode.isAlphanumeric(); got != tt.isAlpha {
t.Errorf("Mode.isAlphanumeric() = %v, want %v", got, tt.isAlpha)
}
if got := tt.mode.isByte(); got != tt.isByte {
t.Errorf("Mode.isByte() = %v, want %v", got, tt.isByte)
}
if got := tt.mode.isKanji(); got != tt.isKanji {
t.Errorf("Mode.isKanji() = %v, want %v", got, tt.isKanji)
}
if got := tt.mode.isEci(); got != tt.isEci {
t.Errorf("Mode.isEci() = %v, want %v", got, tt.isEci)
}
})
}
}

0 comments on commit b32698f

Please sign in to comment.