Skip to content

Commit 81232aa

Browse files
committedAug 8, 2024
Add fizzbuzz example
1 parent 1cfa1aa commit 81232aa

File tree

6 files changed

+211
-3
lines changed

6 files changed

+211
-3
lines changed
 

‎.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11

2-
run.*
3-
bench.*
4-
52
launchSettings.json
63

74
BenchmarkDotNet.Artifacts/

‎.luaurc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"languageMode": "strict",
3+
"lint": {
4+
"*": true
5+
},
6+
"aliases": {
7+
"System": "./lib/System/"
8+
}
9+
}

‎examples/fizzbuzz/FizzBuzz.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
public class FizzBuzz
4+
{
5+
public static void Main(string[] args) {
6+
// Fizz buzz
7+
for (int i = 1; i <= 100; i++)
8+
{
9+
if (i % 15 == 0)
10+
Console.WriteLine("FizzBuzz");
11+
else if (i % 3 == 0)
12+
Console.WriteLine("Fizz");
13+
else if (i % 5 == 0)
14+
Console.WriteLine("Buzz");
15+
else
16+
Console.WriteLine(i);
17+
}
18+
}
19+
}

‎examples/fizzbuzz/FizzBuzz.csproj

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<DebugType>Full</DebugType>
5+
<Optimize>True</Optimize>
6+
<AssemblyName>fizzbuzz</AssemblyName>
7+
<OutputType>Exe</OutputType>
8+
</PropertyGroup>
9+
</Project>

‎examples/fizzbuzz/output.txt

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
1
2+
2
3+
Fizz
4+
4
5+
Buzz
6+
Fizz
7+
7
8+
8
9+
Fizz
10+
Buzz
11+
11
12+
Fizz
13+
13
14+
14
15+
FizzBuzz
16+
16
17+
17
18+
Fizz
19+
19
20+
Buzz
21+
Fizz
22+
22
23+
23
24+
Fizz
25+
Buzz
26+
26
27+
Fizz
28+
28
29+
29
30+
FizzBuzz
31+
31
32+
32
33+
Fizz
34+
34
35+
Buzz
36+
Fizz
37+
37
38+
38
39+
Fizz
40+
Buzz
41+
41
42+
Fizz
43+
43
44+
44
45+
FizzBuzz
46+
46
47+
47
48+
Fizz
49+
49
50+
Buzz
51+
Fizz
52+
52
53+
53
54+
Fizz
55+
Buzz
56+
56
57+
Fizz
58+
58
59+
59
60+
FizzBuzz
61+
61
62+
62
63+
Fizz
64+
64
65+
Buzz
66+
Fizz
67+
67
68+
68
69+
Fizz
70+
Buzz
71+
71
72+
Fizz
73+
73
74+
74
75+
FizzBuzz
76+
76
77+
77
78+
Fizz
79+
79
80+
Buzz
81+
Fizz
82+
82
83+
83
84+
Fizz
85+
Buzz
86+
86
87+
Fizz
88+
88
89+
89
90+
FizzBuzz
91+
91
92+
92
93+
Fizz
94+
94
95+
Buzz
96+
Fizz
97+
97
98+
98
99+
Fizz
100+
Buzz

‎examples/run.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import os
2+
import sys
3+
import subprocess
4+
import shutil
5+
6+
script_path = os.path.abspath(__file__).replace("run.py", "")
7+
examples = [example for example in os.listdir(script_path) if os.path.isdir(os.path.join(script_path, example))]
8+
9+
# Get input project name from user from args
10+
example_name = str(sys.argv[1]).strip().lower()
11+
12+
# Check if project exists
13+
if example_name not in examples:
14+
print(f"Example {example_name} not found")
15+
print(f"Available examples: {examples}")
16+
sys.exit(1)
17+
18+
# Get path to repo root
19+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
20+
21+
cli_path = f"{project_root}/src/CLI/CSynth.CLI.csproj"
22+
example_path = f"{project_root}/examples/{example_name}"
23+
example_dll_path = f"{example_path}/bin/Release/net8.0/{example_name}.dll"
24+
output_path = f"{example_path}/{example_name}.lua"
25+
26+
# Build example and wait for it to finish
27+
os.system(f"dotnet build -c Release {example_path}")
28+
29+
# Run example and capture stdout
30+
result = subprocess.run(["dotnet", "run", "--project", cli_path, "--", example_dll_path], capture_output=True, text=True)
31+
32+
if result.returncode != 0:
33+
print(result.stderr)
34+
sys.exit(1)
35+
36+
# Write to example output
37+
with open(output_path, 'w') as f:
38+
f.write(result.stdout)
39+
40+
print(f"Output written to {output_path}")
41+
42+
# if there's a output.txt file, try compare lua output with it
43+
output_txt_path = f"{example_path}/output.txt"
44+
45+
if os.path.exists(output_txt_path):
46+
with open(output_txt_path, 'r') as f:
47+
expected_output = f.read()
48+
49+
# run example.lua and capture stdout
50+
lune_executable = shutil.which("lune")
51+
52+
if lune_executable:
53+
args = [lune_executable, "run", output_path]
54+
result = subprocess.run(args, capture_output=True, text=True)
55+
else:
56+
print("No lune executable found in path")
57+
sys.exit(1)
58+
59+
if result.returncode != 0:
60+
print(f"Error running {example_name}.lua!")
61+
print(result.stderr)
62+
sys.exit(1)
63+
64+
if result.stdout.strip() == expected_output.strip():
65+
print(f"{example_name}.lua output matches expected output")
66+
else:
67+
print(f"{example_name}.lua output does not match expected output")
68+
print("Expected:")
69+
print(expected_output)
70+
print("Actual:")
71+
print(result.stdout)
72+
else:
73+
print(f"No output.txt file found for {example_name}, won't compare output")
74+

0 commit comments

Comments
 (0)