diff --git a/Tests/p4rseTests/BinaryOperatorTests/AndOr.swift b/Tests/p4rseTests/BinaryOperatorTests/AndOr.swift new file mode 100644 index 0000000..9be81dd --- /dev/null +++ b/Tests/p4rseTests/BinaryOperatorTests/AndOr.swift @@ -0,0 +1,220 @@ +// p4rse, Copyright 202false, Will Hawkins +// +// This file is part of p4rse. +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import Common +import Foundation +import Macros +import P4Lang +import P4Runtime +import SwiftTreeSitter +import Testing +import TreeSitter +import TreeSitterP4 + +@testable import P4Compiler + +// And/Or binary operator tests ... + +@Test func test_simple_parser_binary_operator_and() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true && true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_and2() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (false && false) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_and3() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (false && true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_and4() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true && false) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_or() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true || true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_or2() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true || false) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_or3() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (false || true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_or4() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (false || false) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_grouped() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true && (false || true)) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_grouped2() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true && (false || false)) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} diff --git a/Tests/p4rseTests/BinaryOperatorTests/Bool.swift b/Tests/p4rseTests/BinaryOperatorTests/Bool.swift new file mode 100644 index 0000000..13e3299 --- /dev/null +++ b/Tests/p4rseTests/BinaryOperatorTests/Bool.swift @@ -0,0 +1,296 @@ +// p4rse, Copyright 202false, Will Hawkins +// +// This file is part of p4rse. +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import Common +import Foundation +import Macros +import P4Lang +import P4Runtime +import SwiftTreeSitter +import Testing +import TreeSitter +import TreeSitterP4 + +@testable import P4Compiler + +// Bool binary operator tests ... + +@Test func test_simple_parser_binary_operator_equal_bool() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true == true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_equal_not_equal_bool() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true == false) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_less_than_bool() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (false < true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_less_than_equal_bool() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (false <= true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_less_than_equal_bool2() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true <= true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_greater_than_bool() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true > false) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_greater_than_equal_bool() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (false >= false) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_greater_than_equal_bool2() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true >= false) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_less_than_bool_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true < false) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_less_than_bool_not2() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true < true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_less_than_equal_bool_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true <= false) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_greater_than_bool_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (false > true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_greater_than_bool_not2() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (true > true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_greater_than_equal_bool_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (false >= true) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} diff --git a/Tests/p4rseTests/BinaryOperatorTests/Integer.swift b/Tests/p4rseTests/BinaryOperatorTests/Integer.swift new file mode 100644 index 0000000..2973ac7 --- /dev/null +++ b/Tests/p4rseTests/BinaryOperatorTests/Integer.swift @@ -0,0 +1,296 @@ +// p4rse, Copyright 2026, Will Hawkins +// +// This file is part of p4rse. +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import Common +import Foundation +import Macros +import P4Lang +import P4Runtime +import SwiftTreeSitter +import Testing +import TreeSitter +import TreeSitterP4 + +@testable import P4Compiler + +// Integer binary operator tests ... + +@Test func test_simple_parser_binary_operator_equal_integer() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (5 == 5) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_equal_not_equal_integer() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (5 == 6) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_less_than_integer() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (5 < 6) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_less_than_equal_integer() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (5 <= 6) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_less_than_equal_integer2() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (6 <= 6) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_greater_than_integer() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (7 > 6) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_greater_than_equal_integer() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (6 >= 6) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_greater_than_equal_integer2() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (7 >= 6) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_less_than_integer_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (6 < 5) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_less_than_integer_not2() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (5 < 5) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_less_than_equal_integer_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (6 <= 5) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_greater_than_integer_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (5 > 6) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_greater_than_integer_not2() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (5 > 5) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_greater_than_equal_integer_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select (5 >= 6) { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} diff --git a/Tests/p4rseTests/BinaryOperatorTests/String.swift b/Tests/p4rseTests/BinaryOperatorTests/String.swift new file mode 100644 index 0000000..bf46ad1 --- /dev/null +++ b/Tests/p4rseTests/BinaryOperatorTests/String.swift @@ -0,0 +1,220 @@ +// p4rse, Copyright 2026, Will Hawkins +// +// This file is part of p4rse. +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import Common +import Foundation +import Macros +import P4Lang +import P4Runtime +import SwiftTreeSitter +import Testing +import TreeSitter +import TreeSitterP4 + +@testable import P4Compiler + +// String binary operator tests ... + +@Test func test_simple_parser_binary_operator_equal_string() async throws { + let simple = """ + parser main_parser() { + state start { + transition select ("five" == "five") { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_equal_not_equal_string() async throws { + let simple = """ + parser main_parser() { + state start { + transition select ("five" == "six") { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_less_than_string() async throws { + let simple = """ + parser main_parser() { + state start { + transition select ("five" < "six") { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_less_than_equal_string() async throws { + let simple = """ + parser main_parser() { + state start { + transition select ("six" <= "six") { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_greater_than_string() async throws { + let simple = """ + parser main_parser() { + state start { + transition select ("twelve" > "six") { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_greater_than_equal_string() async throws { + let simple = """ + parser main_parser() { + state start { + transition select ("twelve" >= "six") { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_simple_parser_binary_operator_less_than_string_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select ("six" < "five") { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_less_than_equal_string_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select ("six" <= "five") { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_greater_than_string_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select ("five" > "six") { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} + +@Test func test_simple_parser_binary_operator_greater_than_equal_string_not() async throws { + let simple = """ + parser main_parser() { + state start { + transition select ("five" >= "six") { + true: accept; + false: reject; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +} diff --git a/Tests/p4rseTests/BinaryOperatorTests/Struct.swift b/Tests/p4rseTests/BinaryOperatorTests/Struct.swift new file mode 100644 index 0000000..e89a60c --- /dev/null +++ b/Tests/p4rseTests/BinaryOperatorTests/Struct.swift @@ -0,0 +1,201 @@ +// p4rse, Copyright 2026, Will Hawkins +// +// This file is part of p4rse. +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import Common +import Foundation +import Macros +import P4Lang +import P4Runtime +import SwiftTreeSitter +import Testing +import TreeSitter +import TreeSitterP4 + +@testable import P4Compiler + +@Test func test_struct_equality_empty() async throws { + let simple_parser_declaration = """ + parser main_parser() { + state start { + Testing one; + Testing two; + transition select (one == two) { + true: accept; + false: reject; + }; + } + }; + """ + let test_declarations = VarTypeScopes().enter() + let fields = P4StructFields([ + P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()), + P4StructFieldIdentifier(name: "count", withType: P4Int()), + ]) + var test_types = TypeTypeScopes().enter() + let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) + test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) + + let program = try #UseOkResult( + Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types)) + + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + #expect(state_result == P4Lang.accept) +} + +@Test func test_struct_equality_one_empty() async throws { + let simple_parser_declaration = """ + parser main_parser() { + state start { + Testing one; + Testing two; + + one.yesno = true; + + transition select (one == two) { + true: accept; + false: reject; + }; + } + }; + """ + let test_declarations = VarTypeScopes().enter() + let fields = P4StructFields([ + P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()), + P4StructFieldIdentifier(name: "count", withType: P4Int()), + ]) + var test_types = TypeTypeScopes().enter() + let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) + test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) + + let program = try #UseOkResult( + Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types)) + + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + #expect(state_result == P4Lang.reject) +} + +@Test func test_struct_equality_neither_empty() async throws { + let simple_parser_declaration = """ + parser main_parser() { + state start { + Testing one; + Testing two; + + one.yesno = true; + two.yesno = true; + + transition select (one == two) { + true: accept; + false: reject; + }; + } + }; + """ + let test_declarations = VarTypeScopes().enter() + let fields = P4StructFields([ + P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()), + P4StructFieldIdentifier(name: "count", withType: P4Int()), + ]) + var test_types = TypeTypeScopes().enter() + let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) + test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) + + let program = try #UseOkResult( + Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types)) + + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + #expect(state_result == P4Lang.accept) +} + +@Test func test_struct_equality_neither_empty2() async throws { + let simple_parser_declaration = """ + parser main_parser() { + state start { + Testing one; + Testing two; + + one.yesno = true; + two.yesno = true; + + one.count = 5; + two.count = 5; + + transition select (one == two) { + true: accept; + false: reject; + }; + } + }; + """ + let test_declarations = VarTypeScopes().enter() + let fields = P4StructFields([ + P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()), + P4StructFieldIdentifier(name: "count", withType: P4Int()), + ]) + var test_types = TypeTypeScopes().enter() + let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) + test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) + + let program = try #UseOkResult( + Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types)) + + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + #expect(state_result == P4Lang.accept) +} + +@Test func test_struct_equality_neither_empty3() async throws { + let simple_parser_declaration = """ + parser main_parser() { + state start { + Testing one; + Testing two; + + one.yesno = true; + two.yesno = true; + + one.count = 5; + two.count = 6; + + transition select (one == two) { + true: accept; + false: reject; + }; + } + }; + """ + let test_declarations = VarTypeScopes().enter() + let fields = P4StructFields([ + P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()), + P4StructFieldIdentifier(name: "count", withType: P4Int()), + ]) + var test_types = TypeTypeScopes().enter() + let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) + test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) + + let program = try #UseOkResult( + Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types)) + + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + #expect(state_result == P4Lang.reject) +} + + diff --git a/Tests/p4rseTests/BinaryOperators.swift b/Tests/p4rseTests/BinaryOperators.swift deleted file mode 100644 index 53836ff..0000000 --- a/Tests/p4rseTests/BinaryOperators.swift +++ /dev/null @@ -1,126 +0,0 @@ -// p4rse, Copyright 2026, Will Hawkins -// -// This file is part of p4rse. -// -// This file is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -import Common -import Foundation -import Macros -import P4Lang -import P4Runtime -import SwiftTreeSitter -import Testing -import TreeSitter -import TreeSitterP4 - -@testable import P4Compiler - -@Test func test_simple_parser_binary_operator_equal() async throws { - let simple = """ - parser main_parser() { - state start { - transition select (true == true) { - true: accept; - false: reject; - }; - } - }; - """ - - let program = try #UseOkResult(Program.Compile(simple)) - let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) - let (state_result, _) = try! #UseOkResult(runtime.run()) - - #expect(state_result == P4Lang.accept) -} - -@Test func test_simple_parser_binary_operator_equal_not_equal() async throws { - let simple = """ - parser main_parser() { - state start { - transition select (true == false) { - true: accept; - false: reject; - }; - } - }; - """ - - let program = try #UseOkResult(Program.Compile(simple)) - let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) - let (state_result, _) = try! #UseOkResult(runtime.run()) - - #expect(state_result == P4Lang.reject) -} - -@Test func test_simple_parser_binary_operator_equal_integer() async throws { - let simple = """ - parser main_parser() { - state start { - transition select (5 == 5) { - true: accept; - false: reject; - }; - } - }; - """ - - let program = try #UseOkResult(Program.Compile(simple)) - let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) - let (state_result, _) = try! #UseOkResult(runtime.run()) - - #expect(state_result == P4Lang.accept) -} - -@Test func test_simple_parser_binary_operator_equal_not_equal_integer() async throws { - let simple = """ - parser main_parser() { - state start { - transition select (5 == 6) { - true: accept; - false: reject; - }; - } - }; - """ - - let program = try #UseOkResult(Program.Compile(simple)) - let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) - let (state_result, _) = try! #UseOkResult(runtime.run()) - - #expect(state_result == P4Lang.reject) -} - -@Test func test_simple_parser_binary_operator_equal_invalid_types() async throws { - let simple = """ - parser main_parser() { - state start { - transition select (5 == true) { - true: accept; - false: reject; - }; - } - }; - """ - - let program = try #UseOkResult(Program.Compile(simple)) - let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) - let (state_result, _) = try! #UseOkResult(runtime.run()) - - - // TODO: This test should throw an error. - - #expect(state_result == P4Lang.reject) -} \ No newline at end of file diff --git a/Tests/p4rseTests/ExpressionTests.swift b/Tests/p4rseTests/ExpressionTests.swift new file mode 100644 index 0000000..ed000cc --- /dev/null +++ b/Tests/p4rseTests/ExpressionTests.swift @@ -0,0 +1,88 @@ +// p4rse, Copyright 2026, Will Hawkins +// +// This file is part of p4rse. +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import Common +import Foundation +import Macros +import P4Lang +import P4Runtime +import SwiftTreeSitter +import Testing +import TreeSitter +import TreeSitterP4 + +@testable import P4Compiler + +@Test func test_expression_grouped_equal() async throws { + let simple_parser_declaration = """ + parser main_parser() { + state start { + bool x = (true == (1 < 4)); + transition select (x) { + false: reject; + true: accept; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_expression_grouped_or() async throws { + let simple_parser_declaration = """ + parser main_parser() { + state start { + bool x = (false || (1 < 4)); + transition select (x) { + false: reject; + true: accept; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.accept) +} + +@Test func test_expression_grouped_and() async throws { + let simple_parser_declaration = """ + parser main_parser() { + state start { + bool x = (false && (1 < 4)); + transition select (x) { + false: reject; + true: accept; + }; + } + }; + """ + + let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) + let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program)) + let (state_result, _) = try! #UseOkResult(runtime.run()) + + #expect(state_result == P4Lang.reject) +}