From ca59ff3177ae34ceba2151b977d9fbe398583684 Mon Sep 17 00:00:00 2001 From: gluax Date: Thu, 20 May 2021 14:30:12 -0400 Subject: [PATCH] tests for now should be changed to use input strings when they are in --- compiler/tests/canonicalization/mod.rs | 13 + .../string_transformation.json | 310 ++++++++++++++++++ .../string_transformation.leo | 3 + compiler/tests/type_inference/basic.json | 278 +++++++++++++++- compiler/tests/type_inference/basic.leo | 1 + .../global_consts/global_const_types.leo | 6 +- tests/compiler/string/circuit.leo | 22 ++ tests/compiler/string/equality.leo | 11 + tests/compiler/string/inputs/string.in | 6 + tests/compiler/string/inputs/string_out.in | 6 + .../compiler/compiler/string/circuit.leo.out | 18 + .../compiler/compiler/string/equality.leo.out | 18 + 12 files changed, 688 insertions(+), 4 deletions(-) create mode 100644 compiler/tests/canonicalization/string_transformation.json create mode 100644 compiler/tests/canonicalization/string_transformation.leo create mode 100644 tests/compiler/string/circuit.leo create mode 100644 tests/compiler/string/equality.leo create mode 100644 tests/compiler/string/inputs/string.in create mode 100644 tests/compiler/string/inputs/string_out.in create mode 100644 tests/expectations/compiler/compiler/string/circuit.leo.out create mode 100644 tests/expectations/compiler/compiler/string/equality.leo.out diff --git a/compiler/tests/canonicalization/mod.rs b/compiler/tests/canonicalization/mod.rs index f420b2370c..5a86dcf4b2 100644 --- a/compiler/tests/canonicalization/mod.rs +++ b/compiler/tests/canonicalization/mod.rs @@ -95,3 +95,16 @@ fn test_illegal_array_range_fail() { let program = parse_program(program_string); assert!(program.is_err()); } + +#[test] +fn test_string_transformation() { + let program_string = include_str!("string_transformation.leo"); + let program = parse_program(program_string).unwrap(); + assert_satisfied(program); + + let ast = parse_program_ast(program_string); + let expected_json = include_str!("string_transformation.json"); + let expected_ast: Ast = Ast::from_json_string(expected_json).expect("Unable to parse json."); + + assert_eq!(expected_ast, ast); +} diff --git a/compiler/tests/canonicalization/string_transformation.json b/compiler/tests/canonicalization/string_transformation.json new file mode 100644 index 0000000000..03744195c0 --- /dev/null +++ b/compiler/tests/canonicalization/string_transformation.json @@ -0,0 +1,310 @@ +{ + "name": "", + "expected_input": [], + "imports": [], + "circuits": {}, + "global_consts": {}, + "functions": { + "{\"name\":\"main\",\"span\":\"{\\\"line_start\\\":1,\\\"line_stop\\\":1,\\\"col_start\\\":10,\\\"col_stop\\\":14,\\\"path\\\":\\\"\\\",\\\"content\\\":\\\"function main() {\\\"}\"}": { + "annotations": [], + "identifier": "{\"name\":\"main\",\"span\":\"{\\\"line_start\\\":1,\\\"line_stop\\\":1,\\\"col_start\\\":10,\\\"col_stop\\\":14,\\\"path\\\":\\\"\\\",\\\"content\\\":\\\"function main() {\\\"}\"}", + "input": [], + "output": { + "Tuple": [] + }, + "block": { + "statements": [ + { + "Definition": { + "declaration_type": "Let", + "variable_names": [ + { + "mutable": true, + "identifier": "{\"name\":\"s\",\"span\":\"{\\\"line_start\\\":2,\\\"line_stop\\\":2,\\\"col_start\\\":9,\\\"col_stop\\\":10,\\\"path\\\":\\\"\\\",\\\"content\\\":\\\" let s = `Hello, World!`;\\\"}\"}", + "span": { + "line_start": 2, + "line_stop": 2, + "col_start": 9, + "col_stop": 10, + "path": "", + "content": " let s = `Hello, World!`;" + } + } + ], + "type_": { + "Array": [ + "Char", + [ + { + "value": "13" + } + ] + ] + }, + "value": { + "ArrayInline": { + "elements": [ + { + "Expression": { + "Value": { + "Char": [ + "H", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "e", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "l", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "l", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "o", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + ",", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + " ", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "W", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "o", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "r", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "l", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "d", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "!", + { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + ] + } + } + } + ], + "span": { + "line_start": 2, + "line_stop": 2, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + } + }, + "span": { + "line_start": 2, + "line_stop": 2, + "col_start": 5, + "col_stop": 28, + "path": "", + "content": " let s = `Hello, World!`;" + } + } + } + ], + "span": { + "line_start": 1, + "line_stop": 3, + "col_start": 17, + "col_stop": 2, + "path": "", + "content": "function main() {\n...\n}" + } + }, + "span": { + "line_start": 1, + "line_stop": 3, + "col_start": 1, + "col_stop": 2, + "path": "", + "content": "function main() {\n...\n}" + } + } + } + } + \ No newline at end of file diff --git a/compiler/tests/canonicalization/string_transformation.leo b/compiler/tests/canonicalization/string_transformation.leo new file mode 100644 index 0000000000..0f2e78e1a9 --- /dev/null +++ b/compiler/tests/canonicalization/string_transformation.leo @@ -0,0 +1,3 @@ +function main() { + let s = `Hello, World!`; +} \ No newline at end of file diff --git a/compiler/tests/type_inference/basic.json b/compiler/tests/type_inference/basic.json index 2c6b796cb9..fdd3bf2e15 100644 --- a/compiler/tests/type_inference/basic.json +++ b/compiler/tests/type_inference/basic.json @@ -1027,11 +1027,283 @@ "content": " const n = 'a';" } } + }, + { + "Definition": { + "declaration_type": "Const", + "variable_names": [ + { + "mutable": false, + "identifier": "{\"name\":\"o\",\"span\":\"{\\\"line_start\\\":24,\\\"line_stop\\\":24,\\\"col_start\\\":9,\\\"col_stop\\\":10,\\\"path\\\":\\\"\\\",\\\"content\\\":\\\" const o = `Hello, World!`;\\\"}\"}", + "span": { + "line_start": 24, + "line_stop": 24, + "col_start": 9, + "col_stop": 10, + "path": "", + "content": " const o = `Hello, World!`;" + } + } + ], + "type_": { + "Array": [ + "Char", + [ + { + "value": "13" + } + ] + ] + }, + "value": { + "ArrayInline": { + "elements": [ + { + "Expression": { + "Value": { + "Char": [ + "H", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "e", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "l", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "l", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "o", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + ",", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + " ", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "W", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "o", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "r", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "l", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "d", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "!", + { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + ] + } + } + } + ], + "span": { + "line_start": 24, + "line_stop": 24, + "col_start": 13, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + } + }, + "span": { + "line_start": 24, + "line_stop": 24, + "col_start": 3, + "col_stop": 28, + "path": "", + "content": " const o = `Hello, World!`;" + } + } } ], "span": { "line_start": 9, - "line_stop": 24, + "line_stop": 25, "col_start": 17, "col_stop": 2, "path": "", @@ -1040,11 +1312,11 @@ }, "span": { "line_start": 9, - "line_stop": 24, + "line_stop": 25, "col_start": 1, "col_stop": 2, "path": "", - "content": "function main() {\n...\n}\n\n\n\n\n\n\n\n\n\n\n\n\n" + "content": "function main() {\n...\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n" } } } diff --git a/compiler/tests/type_inference/basic.leo b/compiler/tests/type_inference/basic.leo index 330daeae20..f3dae7a734 100644 --- a/compiler/tests/type_inference/basic.leo +++ b/compiler/tests/type_inference/basic.leo @@ -21,4 +21,5 @@ function main() { const l = (1u8, 1u8, true); const m = Foo {}; const n = 'a'; + const o = `Hello, World!`; } \ No newline at end of file diff --git a/tests/compiler/global_consts/global_const_types.leo b/tests/compiler/global_consts/global_const_types.leo index 5b454dbfc7..5fba7a9316 100644 --- a/tests/compiler/global_consts/global_const_types.leo +++ b/tests/compiler/global_consts/global_const_types.leo @@ -20,6 +20,8 @@ const field_test: field = 2; const use_another_const = basic + 1; const foo = Foo { width: 10, height: 20 }; const uno = uno(); +const character = 'a'; +const hello = `Hello, World!`; circuit Foo { width: u32, @@ -47,5 +49,7 @@ function main(a: u32) -> bool { && use_another_const == 9u32 // use another const test && foo.width == 10u32 // circuit test && foo.height == 20u32 - && uno == 1u32; // function test + && uno == 1u32 // function test + && character == 'a' // char test + && hello == `Hello, World!`; } diff --git a/tests/compiler/string/circuit.leo b/tests/compiler/string/circuit.leo new file mode 100644 index 0000000000..43326849f2 --- /dev/null +++ b/tests/compiler/string/circuit.leo @@ -0,0 +1,22 @@ +/* +namespace: Compile +expectation: Pass +input_file: + - inputs/string_out.in +*/ + +circuit Foo { + s1: [char; 13]; +} + +function takes_string(s: [char; 13]) -> bool { + return s == `Hello, World!`; +} + +function main(s1: [char; 13]) -> [char; 13] { + let f = Foo { s1 }; + let b = takes_string(s1); + + let result = f.s1 == `Hello, World!` ? s1 : `abcdefghjklmn`; + return result; +} \ No newline at end of file diff --git a/tests/compiler/string/equality.leo b/tests/compiler/string/equality.leo new file mode 100644 index 0000000000..460941b18b --- /dev/null +++ b/tests/compiler/string/equality.leo @@ -0,0 +1,11 @@ +/* +namespace: Compile +expectation: Pass +input_file: + - inputs/string.in +*/ + +function main(s1: [char; 13], s2: [char; 4]) -> bool { + let hello: [char; 13] = `Hello, World!`; + return hello == s1 && `nope` != s2; +} \ No newline at end of file diff --git a/tests/compiler/string/inputs/string.in b/tests/compiler/string/inputs/string.in new file mode 100644 index 0000000000..c0d4b5605b --- /dev/null +++ b/tests/compiler/string/inputs/string.in @@ -0,0 +1,6 @@ +[main] +s1: [char; 13] = ['H', 'e', 'l', 'l', 'o', ',', '\u{20}', 'W', 'o', 'r', 'l', 'd', '!']; +s2: [char; 4] = ['t', 'e', 's', 't']; + +[registers] +out: bool = true; \ No newline at end of file diff --git a/tests/compiler/string/inputs/string_out.in b/tests/compiler/string/inputs/string_out.in new file mode 100644 index 0000000000..e3417677af --- /dev/null +++ b/tests/compiler/string/inputs/string_out.in @@ -0,0 +1,6 @@ +[main] +s1: [char; 13] = ['H', 'e', 'l', 'l', 'o', ',', '\u{20}', 'W', 'o', 'r', 'l', 'd', '!']; +s2: [char; 4] = ['t', 'e', 's', 't']; + +[registers] +out: [char; 13] = ['H', 'e', 'l', 'l', 'o', ',', '\u{20}', 'W', 'o', 'r', 'l', 'd', '!']; \ No newline at end of file diff --git a/tests/expectations/compiler/compiler/string/circuit.leo.out b/tests/expectations/compiler/compiler/string/circuit.leo.out new file mode 100644 index 0000000000..b3a7b67aa3 --- /dev/null +++ b/tests/expectations/compiler/compiler/string/circuit.leo.out @@ -0,0 +1,18 @@ +--- +namespace: Compile +expectation: Pass +outputs: + - circuit: + num_public_variables: 0 + num_private_variables: 141 + num_constraints: 115 + at: 145ada587c833434abb89c3349d19e06365fda3eb9b2a227046a78469e3ca313 + bt: f2945a3bc1beaee407bb4ec35303115a93a8c68886d97011cd65ec6d899664e8 + ct: 10b997b6341b3cf811cb7b0fdb891f91006d41c50e9f9566ff92f92816153dfc + output: + - input_file: inputs/string_out.in + output: + registers: + out: + type: "[char; 13]" + value: "Hello, World!" diff --git a/tests/expectations/compiler/compiler/string/equality.leo.out b/tests/expectations/compiler/compiler/string/equality.leo.out new file mode 100644 index 0000000000..21cc0616d7 --- /dev/null +++ b/tests/expectations/compiler/compiler/string/equality.leo.out @@ -0,0 +1,18 @@ +--- +namespace: Compile +expectation: Pass +outputs: + - circuit: + num_public_variables: 0 + num_private_variables: 84 + num_constraints: 67 + at: da464aeb42d53f56ff26141c802d2a769477763766c5746e603c5326b01790bb + bt: 6b03d4cb03e7bf9cf8ec746ee3410578d8ac51a29e56f9090d8e27a4ddf16c64 + ct: ebcd3f740af33d9c3ab2c5e4189709be8d73fab149e788734705cad488a4208c + output: + - input_file: inputs/string.in + output: + registers: + out: + type: bool + value: "true"