Skip to content

Commit 4a244df

Browse files
committed
test: parse all rinha files to json before run tests
1 parent 68ee3d4 commit 4a244df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+95
-1199
lines changed

eval_test.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,69 @@ package main
22

33
import (
44
"encoding/json"
5+
"fmt"
6+
"log"
57
"os"
8+
"os/exec"
69
"strings"
710
"testing"
811
)
912

1013
func TestExamples(t *testing.T) {
1114
files, _ := os.ReadDir("examples")
1215
for _, file := range files {
13-
t.Log(file.Name())
14-
if strings.Contains(file.Name(), ".rinha") {
16+
fileName := file.Name()
17+
t.Log(fileName)
18+
if !strings.Contains(fileName, ".rinha.json") {
19+
rinhaToJson(fileName)
1520
continue
1621
}
22+
}
1723

18-
stdin, err := os.ReadFile("examples/" + file.Name())
24+
files, _ = os.ReadDir("examples")
25+
for _, file := range files {
26+
fileName := file.Name()
27+
t.Log(fileName)
28+
if !strings.Contains(fileName, ".rinha.json") {
29+
continue
30+
}
31+
32+
stdin, err := os.ReadFile("examples/" + fileName)
1933

2034
if err != nil {
2135
panic(err)
2236
}
2337

2438
var ast File
2539
if err := json.Unmarshal(stdin, &ast); err != nil {
40+
fmt.Println(stdin)
2641
t.Error("Error decoding JSON:", err)
2742
return
2843
}
2944
SCOPE_DEFAULT_SIZE := 8
3045
scope := make(Scope, SCOPE_DEFAULT_SIZE)
31-
t.Log(">>>>>>>>>>>> ", file.Name())
46+
t.Log(">>>>>>>>>>>> ", fileName)
3247
Eval(scope, ast.Expression)
3348
t.Log()
3449
}
3550
}
51+
52+
func rinhaToJson(fileName string) {
53+
cmd := exec.Command("rinha", fmt.Sprintf("examples/%s", fileName))
54+
value, err := cmd.Output()
55+
56+
if err != nil {
57+
log.Fatal(err)
58+
}
59+
60+
f, err := os.Create(fmt.Sprintf("examples/%s.json", fileName))
61+
if err != nil {
62+
log.Fatal(err)
63+
}
64+
65+
a, err := f.WriteString(string(value))
66+
if err != nil {
67+
log.Fatal(a, err)
68+
}
69+
f.Sync()
70+
}

examples/add-err.rinha

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/add.json

Lines changed: 0 additions & 61 deletions
This file was deleted.

examples/add.rinha.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"examples/add.rinha","expression":{"kind":"Print","value":{"kind":"Binary","lhs":{"kind":"Str","value":"1 + 1 = ","location":{"start":6,"end":16,"filename":"examples/add.rinha"}},"op":"Add","rhs":{"kind":"Binary","lhs":{"kind":"Int","value":1,"location":{"start":19,"end":20,"filename":"examples/add.rinha"}},"op":"Add","rhs":{"kind":"Int","value":1,"location":{"start":23,"end":24,"filename":"examples/add.rinha"}},"location":{"start":19,"end":24,"filename":"examples/add.rinha"}},"location":{"start":6,"end":24,"filename":"examples/add.rinha"}},"location":{"start":0,"end":25,"filename":"examples/add.rinha"}},"location":{"start":0,"end":25,"filename":"examples/add.rinha"}}

examples/and.json

Lines changed: 0 additions & 43 deletions
This file was deleted.

examples/and.rinha.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"examples/and.rinha","expression":{"kind":"Print","value":{"kind":"Binary","lhs":{"kind":"Int","value":1,"location":{"start":6,"end":7,"filename":"examples/and.rinha"}},"op":"And","rhs":{"kind":"Int","value":2,"location":{"start":11,"end":12,"filename":"examples/and.rinha"}},"location":{"start":6,"end":12,"filename":"examples/and.rinha"}},"location":{"start":0,"end":13,"filename":"examples/and.rinha"}},"location":{"start":0,"end":13,"filename":"examples/and.rinha"}}

examples/div-zero.rinha

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/div.json

Lines changed: 0 additions & 43 deletions
This file was deleted.

examples/div.rinha.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"examples/div.rinha","expression":{"kind":"Print","value":{"kind":"Binary","lhs":{"kind":"Int","value":6,"location":{"start":6,"end":7,"filename":"examples/div.rinha"}},"op":"Div","rhs":{"kind":"Int","value":2,"location":{"start":10,"end":11,"filename":"examples/div.rinha"}},"location":{"start":6,"end":11,"filename":"examples/div.rinha"}},"location":{"start":0,"end":12,"filename":"examples/div.rinha"}},"location":{"start":0,"end":12,"filename":"examples/div.rinha"}}
File renamed without changes.

examples/fac.rinha

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
let factorial = fn (n) => {
2+
if (n == 0) {
3+
1
4+
} else {
5+
n * factorial(n - 1)
6+
}
7+
};
8+
9+
let createTuple = fn (s, n) => {
10+
let repeatedString = fn (str, times) => {
11+
if (times == 1) {
12+
str
13+
} else {
14+
str + repeatedString(str, times - 1)
15+
}
16+
};
17+
18+
(s, repeatedString(s, n))
19+
};
20+
21+
let factResult = factorial(7);
22+
let _ = print("calculated fact");
23+
let _ = print(factResult);
24+
let myTuple = createTuple("yummm memory! ", factResult);
25+
26+
let _ = print(first(myTuple));
27+
print(second(myTuple))

examples/fac.rinha.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"examples/fac.rinha","expression":{"kind":"Let","name":{"text":"factorial","location":{"start":4,"end":13,"filename":"examples/fac.rinha"}},"value":{"kind":"Function","parameters":[{"text":"n","location":{"start":20,"end":21,"filename":"examples/fac.rinha"}}],"value":{"kind":"If","condition":{"kind":"Binary","lhs":{"kind":"Var","text":"n","location":{"start":36,"end":37,"filename":"examples/fac.rinha"}},"op":"Eq","rhs":{"kind":"Int","value":0,"location":{"start":41,"end":42,"filename":"examples/fac.rinha"}},"location":{"start":36,"end":42,"filename":"examples/fac.rinha"}},"then":{"kind":"Int","value":1,"location":{"start":54,"end":55,"filename":"examples/fac.rinha"}},"otherwise":{"kind":"Binary","lhs":{"kind":"Var","text":"n","location":{"start":77,"end":78,"filename":"examples/fac.rinha"}},"op":"Mul","rhs":{"kind":"Call","callee":{"kind":"Var","text":"factorial","location":{"start":81,"end":90,"filename":"examples/fac.rinha"}},"arguments":[{"kind":"Binary","lhs":{"kind":"Var","text":"n","location":{"start":91,"end":92,"filename":"examples/fac.rinha"}},"op":"Sub","rhs":{"kind":"Int","value":1,"location":{"start":95,"end":96,"filename":"examples/fac.rinha"}},"location":{"start":91,"end":96,"filename":"examples/fac.rinha"}}],"location":{"start":81,"end":97,"filename":"examples/fac.rinha"}},"location":{"start":77,"end":97,"filename":"examples/fac.rinha"}},"location":{"start":32,"end":103,"filename":"examples/fac.rinha"}},"location":{"start":16,"end":105,"filename":"examples/fac.rinha"}},"next":{"kind":"Let","name":{"text":"createTuple","location":{"start":112,"end":123,"filename":"examples/fac.rinha"}},"value":{"kind":"Function","parameters":[{"text":"s","location":{"start":130,"end":131,"filename":"examples/fac.rinha"}},{"text":"n","location":{"start":133,"end":134,"filename":"examples/fac.rinha"}}],"value":{"kind":"Let","name":{"text":"repeatedString","location":{"start":149,"end":163,"filename":"examples/fac.rinha"}},"value":{"kind":"Function","parameters":[{"text":"str","location":{"start":170,"end":173,"filename":"examples/fac.rinha"}},{"text":"times","location":{"start":175,"end":180,"filename":"examples/fac.rinha"}}],"value":{"kind":"If","condition":{"kind":"Binary","lhs":{"kind":"Var","text":"times","location":{"start":199,"end":204,"filename":"examples/fac.rinha"}},"op":"Eq","rhs":{"kind":"Int","value":1,"location":{"start":208,"end":209,"filename":"examples/fac.rinha"}},"location":{"start":199,"end":209,"filename":"examples/fac.rinha"}},"then":{"kind":"Var","text":"str","location":{"start":225,"end":228,"filename":"examples/fac.rinha"}},"otherwise":{"kind":"Binary","lhs":{"kind":"Var","text":"str","location":{"start":258,"end":261,"filename":"examples/fac.rinha"}},"op":"Add","rhs":{"kind":"Call","callee":{"kind":"Var","text":"repeatedString","location":{"start":264,"end":278,"filename":"examples/fac.rinha"}},"arguments":[{"kind":"Var","text":"str","location":{"start":279,"end":282,"filename":"examples/fac.rinha"}},{"kind":"Binary","lhs":{"kind":"Var","text":"times","location":{"start":284,"end":289,"filename":"examples/fac.rinha"}},"op":"Sub","rhs":{"kind":"Int","value":1,"location":{"start":292,"end":293,"filename":"examples/fac.rinha"}},"location":{"start":284,"end":293,"filename":"examples/fac.rinha"}}],"location":{"start":264,"end":294,"filename":"examples/fac.rinha"}},"location":{"start":258,"end":294,"filename":"examples/fac.rinha"}},"location":{"start":195,"end":304,"filename":"examples/fac.rinha"}},"location":{"start":166,"end":310,"filename":"examples/fac.rinha"}},"next":{"kind":"Tuple","first":{"kind":"Var","text":"s","location":{"start":322,"end":323,"filename":"examples/fac.rinha"}},"second":{"kind":"Call","callee":{"kind":"Var","text":"repeatedString","location":{"start":325,"end":339,"filename":"examples/fac.rinha"}},"arguments":[{"kind":"Var","text":"s","location":{"start":340,"end":341,"filename":"examples/fac.rinha"}},{"kind":"Var","text":"n","location":{"start":343,"end":344,"filename":"examples/fac.rinha"}}],"location":{"start":325,"end":345,"filename":"examples/fac.rinha"}},"location":{"start":321,"end":346,"filename":"examples/fac.rinha"}},"location":{"start":145,"end":346,"filename":"examples/fac.rinha"}},"location":{"start":126,"end":348,"filename":"examples/fac.rinha"}},"next":{"kind":"Let","name":{"text":"factResult","location":{"start":355,"end":365,"filename":"examples/fac.rinha"}},"value":{"kind":"Call","callee":{"kind":"Var","text":"factorial","location":{"start":368,"end":377,"filename":"examples/fac.rinha"}},"arguments":[{"kind":"Int","value":7,"location":{"start":378,"end":379,"filename":"examples/fac.rinha"}}],"location":{"start":368,"end":380,"filename":"examples/fac.rinha"}},"next":{"kind":"Let","name":{"text":"_","location":{"start":386,"end":387,"filename":"examples/fac.rinha"}},"value":{"kind":"Print","value":{"kind":"Str","value":"calculated fact","location":{"start":396,"end":413,"filename":"examples/fac.rinha"}},"location":{"start":390,"end":414,"filename":"examples/fac.rinha"}},"next":{"kind":"Let","name":{"text":"_","location":{"start":420,"end":421,"filename":"examples/fac.rinha"}},"value":{"kind":"Print","value":{"kind":"Var","text":"factResult","location":{"start":430,"end":440,"filename":"examples/fac.rinha"}},"location":{"start":424,"end":441,"filename":"examples/fac.rinha"}},"next":{"kind":"Let","name":{"text":"myTuple","location":{"start":447,"end":454,"filename":"examples/fac.rinha"}},"value":{"kind":"Call","callee":{"kind":"Var","text":"createTuple","location":{"start":457,"end":468,"filename":"examples/fac.rinha"}},"arguments":[{"kind":"Str","value":"yummm memory! ","location":{"start":469,"end":485,"filename":"examples/fac.rinha"}},{"kind":"Var","text":"factResult","location":{"start":487,"end":497,"filename":"examples/fac.rinha"}}],"location":{"start":457,"end":498,"filename":"examples/fac.rinha"}},"next":{"kind":"Let","name":{"text":"_","location":{"start":505,"end":506,"filename":"examples/fac.rinha"}},"value":{"kind":"Print","value":{"kind":"First","value":{"kind":"Var","text":"myTuple","location":{"start":521,"end":528,"filename":"examples/fac.rinha"}},"location":{"start":515,"end":529,"filename":"examples/fac.rinha"}},"location":{"start":509,"end":530,"filename":"examples/fac.rinha"}},"next":{"kind":"Print","value":{"kind":"Second","value":{"kind":"Var","text":"myTuple","location":{"start":545,"end":552,"filename":"examples/fac.rinha"}},"location":{"start":538,"end":553,"filename":"examples/fac.rinha"}},"location":{"start":532,"end":554,"filename":"examples/fac.rinha"}},"location":{"start":501,"end":554,"filename":"examples/fac.rinha"}},"location":{"start":443,"end":554,"filename":"examples/fac.rinha"}},"location":{"start":416,"end":554,"filename":"examples/fac.rinha"}},"location":{"start":382,"end":554,"filename":"examples/fac.rinha"}},"location":{"start":351,"end":554,"filename":"examples/fac.rinha"}},"location":{"start":108,"end":554,"filename":"examples/fac.rinha"}},"location":{"start":0,"end":554,"filename":"examples/fac.rinha"}},"location":{"start":0,"end":554,"filename":"examples/fac.rinha"}}
File renamed without changes.

0 commit comments

Comments
 (0)