Skip to content

Commit

Permalink
Added separate decode/encode test
Browse files Browse the repository at this point in the history
  • Loading branch information
chiefMarlin committed Apr 11, 2023
1 parent 5951ea9 commit 87086b1
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions viterbi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,42 @@ func TestEncodeDecodeBytes(t *testing.T) {
t.Errorf("Expected %s, got %s", BytesToBits(inputBytes), decoded)
}
}

func TestSeparateEncodeDecoders(t *testing.T) {
// Vars
constraint := 7
polynomials := []int{91, 109, 121}
inputLen := 20
numOfCorruptBits := 1
reversePolynomials := false // Somehow when this is true and using different encode/decode codecs, the test fails

// Generate random input bytes
inputBytes := make([]byte, inputLen)
for i := 0; i < 6; i++ {
inputBytes[i] = byte(rand.Intn(256))
}

codec, err := Init(Input{Constraint: constraint, Polynomials: polynomials, ReversePolynomials: reversePolynomials})
if err != nil {
t.Fatal(err)
}

// Encode
encoded := codec.Encode(BytesToBits(inputBytes))

// Corrupt bits
encoded = corruptBits(encoded, numOfCorruptBits)

decodeCodec, err := Init(Input{Constraint: constraint, Polynomials: polynomials, ReversePolynomials: reversePolynomials})
if err != nil {
t.Fatal(err)
}

// Decode
decoded := decodeCodec.Decode(encoded)

// Check if decoded is equal to input
if !bytes.Equal(BitsToBytes(decoded), inputBytes) {
t.Errorf("Expected %s, got %s", BytesToBits(inputBytes), decoded)
}
}

0 comments on commit 87086b1

Please sign in to comment.