<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/subex/main_test.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2024-12-15 17:54:45 +0000
committerCharlie Stanton <charlie@shtanton.xyz>2024-12-15 17:54:45 +0000
commit62aa738be03845f96c40edde087ea39693b27e4e (patch)
tree81a86faa96db42d9da5e7014f53974468229d3e6 /subex/main_test.go
parentb434fe4e14f6dcc8d1d7433a29351b8e8ea77d37 (diff)
downloadstred-go-numbers.tar
Implement new number systemnumbers
Diffstat (limited to 'subex/main_test.go')
-rw-r--r--subex/main_test.go121
1 files changed, 110 insertions, 11 deletions
diff --git a/subex/main_test.go b/subex/main_test.go
index fb6f152..3855dbc 100644
--- a/subex/main_test.go
+++ b/subex/main_test.go
@@ -36,7 +36,63 @@ func TestSubexMain(t *testing.T) {
tests := []test {
{
- subex: `..+`,
+ // Keep only 5
+ subex: `(5|(.>_))*`,
+ input: []walk.Value {
+ walk.NumberValue(0),
+ walk.NumberValue(1),
+ walk.NumberValue(2),
+ walk.NumberValue(3),
+ walk.NumberValue(4),
+ walk.NumberValue(5),
+ walk.NumberValue(9),
+ walk.NumberValue(10),
+ walk.NumberValue(11),
+ walk.NumberValue(2.5),
+ walk.NumberValue(7.0),
+ walk.NumberValue(-3),
+ },
+ expected: []walk.Value {
+ walk.NumberValue(5),
+ },
+ },
+ {
+ // Keep only odd numbers between 0 and 10
+ subex: `([c5*2+1]|(.>_))*`,
+ input: []walk.Value {
+ walk.NumberValue(0),
+ walk.NumberValue(1),
+ walk.NumberValue(2),
+ walk.NumberValue(3),
+ walk.NumberValue(4),
+ walk.NumberValue(5),
+ walk.NumberValue(9),
+ walk.NumberValue(10),
+ walk.NumberValue(11),
+ walk.NumberValue(2.5),
+ walk.NumberValue(7.0),
+ walk.NumberValue(-3),
+ },
+ expected: []walk.Value {
+ walk.NumberValue(1),
+ walk.NumberValue(3),
+ walk.NumberValue(5),
+ walk.NumberValue(9),
+ walk.NumberValue(7),
+ },
+ },
+ {
+ subex: "r*([pi*2]%a`<a/2`)|([pi*2+1]%b`<b*3+1`)",
+ input: []walk.Value {
+ walk.NumberValue(32),
+ },
+ expected: []walk.Value {
+ walk.NumberValue(32),
+ walk.NumberValue(16),
+ },
+ },
+ {
+ subex: `(..)%+`,
input: []walk.Value {
walk.NumberValue(12),
walk.NumberValue(34),
@@ -70,7 +126,7 @@ func TestSubexMain(t *testing.T) {
},
},
{
- subex: `~(.$_(.{-0}))~`,
+ subex: `~(.>_(.*))~`,
input: []walk.Value {
walk.StringValue("hello"),
},
@@ -79,7 +135,7 @@ func TestSubexMain(t *testing.T) {
},
},
{
- subex: `#(".".{-0})-`,
+ subex: `#(".".*)-`,
input: []walk.Value {
walk.MapValue {
{
@@ -94,7 +150,7 @@ func TestSubexMain(t *testing.T) {
},
},
{
- subex: "@(..$a`$a$a`{-0})@",
+ subex: "@(((..)%a<a)*)@",
input: []walk.Value {
walk.ArrayValue {
walk.ArrayElement {
@@ -221,7 +277,7 @@ func TestSubexMain(t *testing.T) {
},
},
{
- subex: `@(.$_~(.{-0})-{-0})~`,
+ subex: `@((.>_~(.{-0})-){-0})~`,
input: []walk.Value {
walk.ArrayValue {
{
@@ -265,7 +321,7 @@ func TestSubexMain(t *testing.T) {
},
},
{
- subex: ":(.{-0}+)-",
+ subex: ":(.{-0}%+)-",
input: []walk.Value {
walk.ArrayValue {
{
@@ -287,7 +343,7 @@ func TestSubexMain(t *testing.T) {
},
},
{
- subex: "~(-(.)~{-0}):",
+ subex: "~(-(.)~*):",
input: []walk.Value {
walk.StringValue("abc"),
},
@@ -309,7 +365,7 @@ func TestSubexMain(t *testing.T) {
},
},
{
- subex: "#(.(.$_){-0}):",
+ subex: "#((..>_)*):",
input: []walk.Value {
walk.MapValue {
{
@@ -344,7 +400,7 @@ func TestSubexMain(t *testing.T) {
},
},
{
- subex: ":(.`null`{-0})#",
+ subex: ":((.`null`)*)#",
input: []walk.Value {
walk.ArrayValue {
{
@@ -379,7 +435,7 @@ func TestSubexMain(t *testing.T) {
},
},
{
- subex: `#(".$_(.{-0})".{-0})#`,
+ subex: `#((".>_.*".)*)#`,
input: []walk.Value {
walk.MapValue {
{
@@ -406,7 +462,7 @@ func TestSubexMain(t *testing.T) {
},
},
{
- subex: ".{-0}`\"hello\"`",
+ subex: ".*`\"hello\"`",
input: []walk.Value {
walk.NumberValue(1),
walk.NumberValue(2),
@@ -437,3 +493,46 @@ func TestSubexMain(t *testing.T) {
}
}
}
+
+func doCollatzTest(t *testing.T, init int) {
+ input := []walk.Value {
+ walk.NumberValue(init),
+ }
+ last := init
+
+ lexer := NewStringRuneReader("r*([pi*2]%a`<a/2`|[pi*2+1]%b`<b*3+1`)")
+ ast := Parse(lexer)
+ transducer := CompileTransducer(ast)
+
+ for last != 1 {
+ output, err := RunTransducer(transducer, input)
+
+ if err {
+ t.Errorf("Collatz rejected %v", input)
+ return
+ }
+
+ if last % 2 == 0 {
+ last = last / 2
+ } else {
+ last = last * 3 + 1
+ }
+
+ if !reflect.DeepEqual(append(input, walk.NumberValue(last)), output) {
+ t.Errorf("Collatz took input: %v and produced output %v", input, output)
+ return
+ }
+
+ input = output
+ }
+
+ output, err := RunTransducer(transducer, input)
+ if !err {
+ t.Errorf("Collatz accepted input %v. Produced output: %v", input, output)
+ }
+}
+
+func TestSubexCollatz(t *testing.T) {
+ doCollatzTest(t, 32)
+ doCollatzTest(t, 7)
+}