rewrite in go and compile to wasm

This commit is contained in:
2024-10-24 18:07:10 +00:00
parent bc50e3ee4d
commit 547dffd8ee
18 changed files with 810 additions and 468 deletions

610
test/sequences Normal file

File diff suppressed because one or more lines are too long

305
test/test_affine_p0_sol Normal file

File diff suppressed because one or more lines are too long

305
test/test_affine_p1_sol Normal file

File diff suppressed because one or more lines are too long

305
test/test_affine_p2_sol Normal file

File diff suppressed because one or more lines are too long

29
test/tests.json Normal file
View File

@@ -0,0 +1,29 @@
{
"p0": {
"penalties": {
"m": 0,
"x": 1,
"o": 2,
"e": 1
},
"solutions": "test_affine_p0_sol"
},
"p1": {
"penalties": {
"m": 0,
"x": 3,
"o": 1,
"e": 4
},
"solutions": "test_affine_p1_sol"
},
"p2": {
"penalties": {
"m": 0,
"x": 5,
"o": 3,
"e": 2
},
"solutions": "test_affine_p2_sol"
}
}

78
test/wfa_test.go Normal file
View File

@@ -0,0 +1,78 @@
package tests
import (
"bufio"
"encoding/json"
"os"
"strconv"
"strings"
"testing"
wfa "wfa/pkg"
"github.com/schollz/progressbar/v3"
)
const testJsonPath = "tests.json"
const testSequences = "sequences"
type TestPenalty struct {
M int `json:"m"`
X int `json:"x"`
O int `json:"o"`
E int `json:"e"`
}
type TestCase struct {
Penalties TestPenalty `json:"penalties"`
Solutions string `json:"solutions"`
}
func TestWFA(t *testing.T) {
content, _ := os.ReadFile(testJsonPath)
var testMap map[string]TestCase
json.Unmarshal(content, &testMap)
for k, v := range testMap {
testName := k
testPenalties := wfa.Penalty{
M: v.Penalties.M,
X: v.Penalties.X,
O: v.Penalties.O,
E: v.Penalties.E,
}
sequencesFile, _ := os.Open(testSequences)
sequences := bufio.NewScanner(sequencesFile)
solutionsFile, _ := os.Open(v.Solutions)
solutions := bufio.NewScanner(solutionsFile)
bar := progressbar.Default(305, k)
idx := 0
for solutions.Scan() {
solution := solutions.Text()
expectedScore, _ := strconv.Atoi(strings.Split(solution, "\t")[0])
sequences.Scan()
s1 := sequences.Text()
s1 = s1[1:]
sequences.Scan()
s2 := sequences.Text()
s2 = s2[1:]
x := wfa.WFAlign(s1, s2, testPenalties, false)
gotScore := x.Score
if gotScore != -1*expectedScore {
t.Errorf(`test: %s#%d, s1: %s, s2: %s, got: %d, expected: %d\n`, testName, idx, s1, s2, gotScore, expectedScore)
}
idx++
bar.Add(1)
}
}
}