Skip to content

Commit

Permalink
CU-86a1hur7f - Refactor test_relational.py to use BoaTestConstructor
Browse files Browse the repository at this point in the history
  • Loading branch information
luc10921 committed Feb 8, 2024
1 parent dcad06a commit 1dc12e6
Show file tree
Hide file tree
Showing 11 changed files with 589 additions and 723 deletions.
10 changes: 9 additions & 1 deletion boa3/internal/model/operation/binary/relational/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
'NumericEquality',
'NumericInequality',
'ObjectEquality',
'ObjectInequality'
'ObjectInequality',
'StrBytesGreaterThan',
'StrBytesGreaterThanOrEqual',
'StrBytesLessThan',
'StrBytesLessThanOrEqual',
]

from boa3.internal.model.operation.binary.relational.greaterthan import GreaterThan
Expand All @@ -20,3 +24,7 @@
from boa3.internal.model.operation.binary.relational.numericinequality import NumericInequality
from boa3.internal.model.operation.binary.relational.objectequality import ObjectEquality
from boa3.internal.model.operation.binary.relational.objectinequality import ObjectInequality
from boa3.internal.model.operation.binary.relational.strbytesgreaterthan import StrBytesGreaterThan
from boa3.internal.model.operation.binary.relational.strbytesgreaterthanorequal import StrBytesGreaterThanOrEqual
from boa3.internal.model.operation.binary.relational.strbyteslessthan import StrBytesLessThan
from boa3.internal.model.operation.binary.relational.strbyteslessthanorequal import StrBytesLessThanOrEqual
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GreaterThan(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.int, Type.str]
_valid_types: List[IType] = [Type.int]

def __init__(self, left: IType = Type.int, right: IType = None):
self.operator: Operator = Operator.Gt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GreaterThanOrEqual(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.int, Type.str]
_valid_types: List[IType] = [Type.int]

def __init__(self, left: IType = Type.int, right: IType = None):
self.operator: Operator = Operator.GtE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LessThan(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.int, Type.str]
_valid_types: List[IType] = [Type.int]

def __init__(self, left: IType = Type.int, right: IType = None):
self.operator: Operator = Operator.Lt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LessThanOrEqual(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.int, Type.str]
_valid_types: List[IType] = [Type.int]

def __init__(self, left: IType = Type.int, right: IType = None):
self.operator: Operator = Operator.LtE
Expand Down
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)
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)
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)
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)
4 changes: 4 additions & 0 deletions boa3/internal/model/operation/binaryop.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class BinaryOp:
IsNot = NotIdentity()
Eq = ObjectEquality()
NotEq = ObjectInequality()
StrBytesGt = StrBytesGreaterThan()
StrBytesGtE = StrBytesGreaterThanOrEqual()
StrBytesLt = StrBytesLessThan()
StrBytesLtE = StrBytesLessThanOrEqual()

# Logical operations
And = BooleanAnd()
Expand Down

0 comments on commit 1dc12e6

Please sign in to comment.