Skip to content

Commit

Permalink
added unit tests for reachable panics.
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyStiles committed Jan 2, 2024
1 parent 69c5089 commit 94f2a15
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
6 changes: 3 additions & 3 deletions bwt/bwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ func (bwt BWT) lookupSkipByOffset(offset int) skipEntry {
panic(msg)
}

for i := range bwt.firstColumnSkipList {
if bwt.firstColumnSkipList[i].openEndedInterval.start <= offset && offset < bwt.firstColumnSkipList[i].openEndedInterval.end {
return bwt.firstColumnSkipList[i]
for skipIndex := range bwt.firstColumnSkipList {
if bwt.firstColumnSkipList[skipIndex].openEndedInterval.start <= offset && offset < bwt.firstColumnSkipList[skipIndex].openEndedInterval.end {
return bwt.firstColumnSkipList[skipIndex]
}
}
panic("figure out what to do here")
Expand Down
87 changes: 87 additions & 0 deletions bwt/bwt_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bwt

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -323,3 +324,89 @@ func TestBWTReconstruction(t *testing.T) {
t.Fail()
}
}

func TestBWTStartError(t *testing.T) {
testStr := "banana"

bwt, err := New(testStr)
if err != nil {
t.Fatal(err)
}

_, err = bwt.Extract(-1, 6)
if err == nil {
t.Fatal("expected error but got nil")
}
}
func TestBWT_GetFCharPosFromOriginalSequenceCharPos_Panic(t *testing.T) {
testStr := "banana"
bwt, err := New(testStr)
if err != nil {
t.Fatal(err)
}

// Call the function with an invalid original position
originalPos := -1
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic, but it did not occur")
}
}()
bwt.getFCharPosFromOriginalSequenceCharPos(originalPos)
}
func TestBWT_LFSearch_InvalidChar(t *testing.T) {
testStr := "banana"
bwt, err := New(testStr)
if err != nil {
t.Fatal(err)
}

pattern := "x" // Invalid character

result := bwt.lfSearch(pattern)

if result.start != 0 || result.end != 0 {
t.Fatalf("Expected search range to be (0, 0), but got (%d, %d)", result.start, result.end)
}
}
func TestBWT_LookupSkipByOffset_PanicOffsetExceedsMaxBound(t *testing.T) {
testStr := "banana"
bwt, err := New(testStr)
if err != nil {
t.Fatal(err)
}

offset := bwt.getLenOfOriginalStringWithNullChar()
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic, but it did not occur")
}
}()
bwt.lookupSkipByOffset(offset)
}

func TestBWT_LookupSkipByOffset_PanicOffsetExceedsMinBound(t *testing.T) {
testStr := "banana"
bwt, err := New(testStr)
if err != nil {
t.Fatal(err)
}

offset := -1
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic, but it did not occur")
}
}()
bwt.lookupSkipByOffset(offset)
}

func TestBWTRecovery(t *testing.T) {
// Test panic recovery for bwtRecovery function
err := fmt.Errorf("test error")
operation := "test operation"
defer bwtRecovery(operation, &err)

panic("test panic")
t.Errorf("Expected panic, but it did not occur")

Check failure on line 411 in bwt/bwt_test.go

View workflow job for this annotation

GitHub Actions / lint

unreachable: unreachable code (govet)
}
23 changes: 23 additions & 0 deletions bwt/wavelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,26 @@ func TestWaveletTreeSingleAlpha(t *testing.T) {
t.Fatalf("expected Access(%d) to be %d but got %d", 1, 1, s)
}
}
func TestBuildWaveletTree_ZeroAlpha(t *testing.T) {
bytes := []byte("AAAACCCCTTTTGGGG")
alpha := []charInfo{}

root := buildWaveletTree(0, alpha, bytes)

if root != nil {
t.Fatalf("expected root to be nil but got %v", root)
}
}
func TestWaveletTree_LookupCharInfo_Panic(t *testing.T) {
wt := waveletTree{
alpha: []charInfo{},
}

defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic but got nil")
}
}()

wt.lookupCharInfo('B')
}

0 comments on commit 94f2a15

Please sign in to comment.