Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 9 additions & 32 deletions services/app/apps/runner/lib/runner/checker_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,6 @@ defmodule Runner.CheckerGenerator do
)
end

defp get_value_expression(
signature = %{type: %{nested: _nested}},
value,
lang_meta = %{checker_meta: checker_meta}
) do
type_name = TypesGenerator.call(signature.type, lang_meta)
type = extract_type(signature)
value = get_value({type, value}, lang_meta)

EEx.eval_string(checker_meta.nested_value_expression_template,
value: value,
type_name: type_name
)
end

defp get_value_expression(signature, value, lang_meta) do
type = extract_type(signature)
get_value({type, value}, lang_meta)
Expand All @@ -130,21 +115,6 @@ defmodule Runner.CheckerGenerator do
defp get_value({%{name: "boolean"}, value}, %{checker_meta: checker_meta}),
do: get_boolean_value(checker_meta.type_templates, value)

# Special case for CSharp list of lists
defp get_value(
{%{name: "array", nested: %{name: "array", nested: nested}}, [value]},
lang_meta = %{slug: "csharp", checker_meta: checker_meta}
) do
inner_type = TypesGenerator.call(nested, lang_meta)
array_values = Enum.map_join(value, ", ", &get_value({nested, &1}, lang_meta))

EEx.eval_string(checker_meta.type_templates.array_of_array,
type: inner_type,
entries: array_values,
inner_type: inner_type
)
end

defp get_value(
{%{name: "array", nested: nested}, value},
lang_meta = %{checker_meta: checker_meta}
Expand All @@ -158,16 +128,23 @@ defmodule Runner.CheckerGenerator do
)
end

defp get_value({signature = %{name: "hash"}, value}, lang_meta = %{checker_meta: checker_meta}) do
defp get_value(
{signature = %{name: "hash", nested: nested}, value},
lang_meta = %{checker_meta: checker_meta}
) do
list = Map.to_list(value)
inner_type = TypesGenerator.call(nested, lang_meta)

if Enum.empty?(list) do
checker_meta.type_templates.hash_empty
else
hash_entries =
Enum.map_join(list, ", ", fn item -> get_hash_inners(item, signature, lang_meta) end)

EEx.eval_string(checker_meta.type_templates.hash_value, entries: hash_entries)
EEx.eval_string(checker_meta.type_templates.hash_value,
entries: hash_entries,
inner_type: inner_type
)
end
end

Expand Down
21 changes: 12 additions & 9 deletions services/app/apps/runner/lib/runner/languages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ defmodule Runner.Languages do
"integer" => "0",
"float" => "0.1",
"string" => "\"value\"",
"array" => "new List<<%= value %>>()",
"array" => "new()",
"boolean" => "true",
"hash" => "new Dictionary<string, <%= value %>>(){ {\"key\", <%= value %>} }"
"hash" => "new()"
},
expected_template: " <%= type %>",
types: %{
Expand All @@ -412,10 +412,10 @@ defmodule Runner.Languages do
version: :static,
type_templates: %{
@type_templates
| array: "{<%= entries %>}",
| array: "new() {<%= entries %>}",
array_of_array: "{new List<<%= type %>> {<%= entries %>}}",
hash_empty: "{}",
hash_value: "{<%= entries %>}",
hash_empty: "new()",
hash_value: "new() {<%= entries %>}",
hash_inners: "{\"<%= key %>\", <%= value %>}"
},
defining_variable_template: "<%= type %> <%= name %>",
Expand All @@ -437,9 +437,8 @@ defmodule Runner.Languages do
// import "fmt"

func solution(<%= arguments %>)<%= expected %> {
var ans <%= expected %>
ans = <%= default_value %>
return ans
var ans <%= expected %>
return ans
}
// <%= comment %>
""",
Expand All @@ -463,7 +462,11 @@ defmodule Runner.Languages do
},
checker_meta: %{
version: :static,
type_templates: %{@type_templates | array: "{<%= entries %>}"},
type_templates: %{
@type_templates
| array: "[]<%= inner_type %>{<%= entries %>}",
hash_value: "map[string]<%= inner_type %>{<%= entries %>}"
},
defining_variable_template: "<%= name %> <%= type %>",
nested_value_expression_template: "<%= type_name %><%= value %>"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Runner.SolutionGeneratorTest do
{
public List<string> solution(int a, string text, double b, bool c, Dictionary<string, string> nested_hash_of_string, List<string> nested_array_of_string, List<List<string>> nested_array_of_array_of_strings)
{
List<string> ans = new List<"value">();
List<string> ans = new();
return ans;
}
}
Expand Down Expand Up @@ -63,9 +63,8 @@ defmodule Runner.SolutionGeneratorTest do
// import "fmt"

func solution(a int64, text string, b float64, c bool, nested_hash_of_string map[string]string, nested_array_of_string []string, nested_array_of_array_of_strings [][]string) []string {
var ans []string
ans = []"value"{}
return ans
var ans []string
return ans
}
// use stdout to debug
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
defmodule Runner.SolutionGeneratorTypeTests.CppTypeTest do
use ExUnit.Case, async: true

alias Runner.Languages
alias Runner.SolutionGenerator

# Test for string input and output
test "cpp with string input and output" do
task = %Runner.Task{
asserts: [
%{
arguments: ["hello"],
expected: "world"
}
],
input_signature: [
%{
argument_name: "input_string",
type: %{name: "string"}
}
],
output_signature: %{
type: %{name: "string"}
}
}

expected = """
#include <bits/stdc++.h>

using namespace std;

string solution(string input_string) {
string ans;
ans = "value";
return ans;
}
// use stdout to debug
"""

assert String.trim_trailing(expected, "\n") ==
SolutionGenerator.call(task, Languages.meta("cpp"))
end

# Test for integer input and output
test "cpp with integer input and output" do
task = %Runner.Task{
asserts: [
%{
arguments: [42],
expected: 84
}
],
input_signature: [
%{
argument_name: "input_integer",
type: %{name: "integer"}
}
],
output_signature: %{
type: %{name: "integer"}
}
}

expected = """
#include <bits/stdc++.h>

using namespace std;

int solution(int input_integer) {
int ans;
ans = 0;
return ans;
}
// use stdout to debug
"""

assert String.trim_trailing(expected, "\n") ==
SolutionGenerator.call(task, Languages.meta("cpp"))
end

# Test for array of string input and output
test "cpp with array of string input and output" do
task = %Runner.Task{
asserts: [
%{
arguments: [["hello", "world"]],
expected: ["world", "hello"]
}
],
input_signature: [
%{
argument_name: "input_array",
type: %{name: "array", nested: %{name: "string"}}
}
],
output_signature: %{
type: %{name: "array", nested: %{name: "string"}}
}
}

expected = """
#include <bits/stdc++.h>

using namespace std;

vector<string> solution(vector<string> input_array) {
vector<string> ans;
ans = {"value"};
return ans;
}
// use stdout to debug
"""

assert String.trim_trailing(expected, "\n") ==
SolutionGenerator.call(task, Languages.meta("cpp"))
end

# Test for hash of string input and output
test "cpp with hash of string input and output" do
task = %Runner.Task{
asserts: [
%{
arguments: [%{"key1" => "value1", "key2" => "value2"}],
expected: %{"key1" => "value1", "key2" => "value2"}
}
],
input_signature: [
%{
argument_name: "input_hash",
type: %{name: "hash", nested: %{name: "string"}}
}
],
output_signature: %{
type: %{name: "hash", nested: %{name: "string"}}
}
}

expected = """
#include <bits/stdc++.h>

using namespace std;

map<string,string> solution(map<string,string> input_hash) {
map<string,string> ans;
ans = {{"key", "value"}};
return ans;
}
// use stdout to debug
"""

assert String.trim_trailing(expected, "\n") ==
SolutionGenerator.call(task, Languages.meta("cpp"))
end

# Test for nested array input and output
test "cpp with nested array input and output" do
task = %Runner.Task{
asserts: [
%{
arguments: [[["hello", "world"], ["foo", "bar"]]],
expected: [["world", "hello"], ["bar", "foo"]]
}
],
input_signature: [
%{
argument_name: "input_array",
type: %{name: "array", nested: %{name: "array", nested: %{name: "string"}}}
}
],
output_signature: %{
type: %{name: "array", nested: %{name: "array", nested: %{name: "string"}}}
}
}

expected = """
#include <bits/stdc++.h>

using namespace std;

vector<vector<string>> solution(vector<vector<string>> input_array) {
vector<vector<string>> ans;
ans = {{"value"}};
return ans;
}
// use stdout to debug
"""

assert String.trim_trailing(expected, "\n") ==
SolutionGenerator.call(task, Languages.meta("cpp"))
end

# Test for complex nested type
test "cpp with complex nested type input and output" do
task = %Runner.Task{
asserts: [
%{
arguments: [[%{"outer1" => %{"inner1" => ["a", "b"]}}]],
expected: [[%{"outer1" => %{"inner1" => ["A", "B"]}}]]
}
],
input_signature: [
%{
argument_name: "input_complex",
type: %{name: "array", nested: %{name: "hash", nested: %{name: "hash", nested: %{name: "array", nested: %{name: "string"}}}}}
}
],
output_signature: %{
type: %{name: "array", nested: %{name: "hash", nested: %{name: "hash", nested: %{name: "array", nested: %{name: "string"}}}}}
}
}

expected = """
#include <bits/stdc++.h>

using namespace std;

vector<map<string,map<string,vector<string>>>> solution(vector<map<string,map<string,vector<string>>>> input_complex) {
vector<map<string,map<string,vector<string>>>> ans;
ans = {{{"key", {{"key", {"value"}}}}}};
return ans;
}
// use stdout to debug
"""

assert String.trim_trailing(expected, "\n") ==
SolutionGenerator.call(task, Languages.meta("cpp"))
end
end
Loading
Loading