-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CU-86a1hur7f - Refactor test_relational.py to use BoaTestConstructor
- Loading branch information
luc10921
committed
Feb 8, 2024
1 parent
dcad06a
commit 1dc12e6
Showing
11 changed files
with
589 additions
and
723 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
boa3/internal/model/operation/binary/relational/strbytesgreaterthan.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import List | ||
|
||
from boa3.internal.model.builtin.interop.interop import Interop | ||
from boa3.internal.model.operation.binary.binaryoperation import BinaryOperation | ||
from boa3.internal.model.operation.operator import Operator | ||
from boa3.internal.model.type.type import IType, Type | ||
from boa3.internal.neo.vm.opcode.Opcode import Opcode | ||
|
||
|
||
class StrBytesGreaterThan(BinaryOperation): | ||
""" | ||
A class used to represent a string/bytes greater than comparison | ||
:ivar operator: the operator of the operation. Inherited from :class:`IOperation` | ||
:ivar left: the left operand type. Inherited from :class:`BinaryOperation` | ||
:ivar right: the left operand type. Inherited from :class:`BinaryOperation` | ||
:ivar result: the result type of the operation. Inherited from :class:`IOperation` | ||
""" | ||
_valid_types: List[IType] = [Type.bytes, Type.str] | ||
|
||
def __init__(self, left: IType = Type.str, right: IType = None): | ||
self.operator: Operator = Operator.Gt | ||
super().__init__(left, right) | ||
|
||
def validate_type(self, *types: IType) -> bool: | ||
if len(types) != self.number_of_operands: | ||
return False | ||
left: IType = types[0] | ||
right: IType = types[1] | ||
|
||
return left == right and any(_type.is_type_of(left) for _type in self._valid_types) | ||
|
||
def _get_result(self, left: IType, right: IType) -> IType: | ||
if self.validate_type(left, right): | ||
return Type.bool | ||
else: | ||
return Type.none | ||
|
||
def generate_internal_opcodes(self, code_generator): | ||
# comparing strings, on the stack the first string is the right one and the second is the left one | ||
Interop.MemoryCompare.generate_internal_opcodes(code_generator) | ||
# -1 means the left string is greater than the right string | ||
# 1 means the left string is less than the right string | ||
# 0 means the right string is equals to the left string | ||
|
||
# if comparison equals -1 (the left string is greater than the right string), then return True | ||
code_generator.convert_literal(-1) | ||
if_comparison_equals_m1 = code_generator.convert_begin_if() | ||
code_generator.change_jump(if_comparison_equals_m1, Opcode.JMPNE) | ||
code_generator.convert_literal(True) | ||
|
||
# else, return False | ||
if_comparison_not_equals_m1 = code_generator.convert_begin_else(if_comparison_equals_m1) | ||
code_generator.convert_literal(False) | ||
code_generator.convert_end_if(if_comparison_not_equals_m1) |
55 changes: 55 additions & 0 deletions
55
boa3/internal/model/operation/binary/relational/strbytesgreaterthanorequal.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import List | ||
|
||
from boa3.internal.model.builtin.interop.interop import Interop | ||
from boa3.internal.model.operation.binary.binaryoperation import BinaryOperation | ||
from boa3.internal.model.operation.operator import Operator | ||
from boa3.internal.model.type.type import IType, Type | ||
from boa3.internal.neo.vm.opcode.Opcode import Opcode | ||
|
||
|
||
class StrBytesGreaterThanOrEqual(BinaryOperation): | ||
""" | ||
A class used to represent a string/bytes greater than or equal comparison | ||
:ivar operator: the operator of the operation. Inherited from :class:`IOperation` | ||
:ivar left: the left operand type. Inherited from :class:`BinaryOperation` | ||
:ivar right: the left operand type. Inherited from :class:`BinaryOperation` | ||
:ivar result: the result type of the operation. Inherited from :class:`IOperation` | ||
""" | ||
_valid_types: List[IType] = [Type.bytes, Type.str] | ||
|
||
def __init__(self, left: IType = Type.str, right: IType = None): | ||
self.operator: Operator = Operator.GtE | ||
super().__init__(left, right) | ||
|
||
def validate_type(self, *types: IType) -> bool: | ||
if len(types) != self.number_of_operands: | ||
return False | ||
left: IType = types[0] | ||
right: IType = types[1] | ||
|
||
return left == right and any(_type.is_type_of(left) for _type in self._valid_types) | ||
|
||
def _get_result(self, left: IType, right: IType) -> IType: | ||
if self.validate_type(left, right): | ||
return Type.bool | ||
else: | ||
return Type.none | ||
|
||
def generate_internal_opcodes(self, code_generator): | ||
# comparing strings, on the stack the first string is the right one and the second is the left one | ||
Interop.MemoryCompare.generate_internal_opcodes(code_generator) | ||
# -1 means the left string is greater than the right string | ||
# 1 means the left string is less than the right string | ||
# 0 means the right string is equals to the left string | ||
|
||
# if comparison equals 1 (the left string is less than the right string), then return False | ||
code_generator.convert_literal(1) | ||
if_comparison_equals_1 = code_generator.convert_begin_if() | ||
code_generator.change_jump(if_comparison_equals_1, Opcode.JMPNE) | ||
code_generator.convert_literal(False) | ||
|
||
# else, return True | ||
if_comparison_not_equals_1 = code_generator.convert_begin_else(if_comparison_equals_1) | ||
code_generator.convert_literal(True) | ||
code_generator.convert_end_if(if_comparison_not_equals_1) |
55 changes: 55 additions & 0 deletions
55
boa3/internal/model/operation/binary/relational/strbyteslessthan.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import List | ||
|
||
from boa3.internal.model.builtin.interop.interop import Interop | ||
from boa3.internal.model.operation.binary.binaryoperation import BinaryOperation | ||
from boa3.internal.model.operation.operator import Operator | ||
from boa3.internal.model.type.type import IType, Type | ||
from boa3.internal.neo.vm.opcode.Opcode import Opcode | ||
|
||
|
||
class StrBytesLessThan(BinaryOperation): | ||
""" | ||
A class used to represent a string/bytes less than comparison | ||
:ivar operator: the operator of the operation. Inherited from :class:`IOperation` | ||
:ivar left: the left operand type. Inherited from :class:`BinaryOperation` | ||
:ivar right: the left operand type. Inherited from :class:`BinaryOperation` | ||
:ivar result: the result type of the operation. Inherited from :class:`IOperation` | ||
""" | ||
_valid_types: List[IType] = [Type.bytes, Type.str] | ||
|
||
def __init__(self, left: IType = Type.str, right: IType = None): | ||
self.operator: Operator = Operator.Lt | ||
super().__init__(left, right) | ||
|
||
def validate_type(self, *types: IType) -> bool: | ||
if len(types) != self.number_of_operands: | ||
return False | ||
left: IType = types[0] | ||
right: IType = types[1] | ||
|
||
return left == right and any(_type.is_type_of(left) for _type in self._valid_types) | ||
|
||
def _get_result(self, left: IType, right: IType) -> IType: | ||
if self.validate_type(left, right): | ||
return Type.bool | ||
else: | ||
return Type.none | ||
|
||
def generate_internal_opcodes(self, code_generator): | ||
# comparing strings, on the stack the first string is the right one and the second is the left one | ||
Interop.MemoryCompare.generate_internal_opcodes(code_generator) | ||
# -1 means the left string is greater than the right string | ||
# 1 means the left string is less than the right string | ||
# 0 means the right string is equals to the left string | ||
|
||
# if comparison equals 1 (the left string is less than the right string), then return True | ||
code_generator.convert_literal(1) | ||
if_comparison_equals_1 = code_generator.convert_begin_if() | ||
code_generator.change_jump(if_comparison_equals_1, Opcode.JMPNE) | ||
code_generator.convert_literal(True) | ||
|
||
# else, return False | ||
if_comparison_not_equals_1 = code_generator.convert_begin_else(if_comparison_equals_1) | ||
code_generator.convert_literal(False) | ||
code_generator.convert_end_if(if_comparison_not_equals_1) |
55 changes: 55 additions & 0 deletions
55
boa3/internal/model/operation/binary/relational/strbyteslessthanorequal.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import List | ||
|
||
from boa3.internal.model.builtin.interop.interop import Interop | ||
from boa3.internal.model.operation.binary.binaryoperation import BinaryOperation | ||
from boa3.internal.model.operation.operator import Operator | ||
from boa3.internal.model.type.type import IType, Type | ||
from boa3.internal.neo.vm.opcode.Opcode import Opcode | ||
|
||
|
||
class StrBytesLessThanOrEqual(BinaryOperation): | ||
""" | ||
A class used to represent a string/bytes less than or equal comparison | ||
:ivar operator: the operator of the operation. Inherited from :class:`IOperation` | ||
:ivar left: the left operand type. Inherited from :class:`BinaryOperation` | ||
:ivar right: the left operand type. Inherited from :class:`BinaryOperation` | ||
:ivar result: the result type of the operation. Inherited from :class:`IOperation` | ||
""" | ||
_valid_types: List[IType] = [Type.bytes, Type.str] | ||
|
||
def __init__(self, left: IType = Type.str, right: IType = None): | ||
self.operator: Operator = Operator.LtE | ||
super().__init__(left, right) | ||
|
||
def validate_type(self, *types: IType) -> bool: | ||
if len(types) != self.number_of_operands: | ||
return False | ||
left: IType = types[0] | ||
right: IType = types[1] | ||
|
||
return left == right and any(_type.is_type_of(left) for _type in self._valid_types) | ||
|
||
def _get_result(self, left: IType, right: IType) -> IType: | ||
if self.validate_type(left, right): | ||
return Type.bool | ||
else: | ||
return Type.none | ||
|
||
def generate_internal_opcodes(self, code_generator): | ||
# comparing strings, on the stack the first string is the right one and the second is the left one | ||
Interop.MemoryCompare.generate_internal_opcodes(code_generator) | ||
# -1 means the left string is greater than the right string | ||
# 1 means the left string is less than the right string | ||
# 0 means the right string is equals to the left string | ||
|
||
# if comparison equals -1 (the left string is greater than the right string), then return False | ||
code_generator.convert_literal(-1) | ||
if_comparison_equals_m1 = code_generator.convert_begin_if() | ||
code_generator.change_jump(if_comparison_equals_m1, Opcode.JMPNE) | ||
code_generator.convert_literal(False) | ||
|
||
# else, return True | ||
if_comparison_not_equals_m1 = code_generator.convert_begin_else(if_comparison_equals_m1) | ||
code_generator.convert_literal(True) | ||
code_generator.convert_end_if(if_comparison_not_equals_m1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.