• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CityOfZion / neo3-boa / 858f5658-b835-4f01-86b4-3a69cebd0b86

16 Oct 2023 06:23PM UTC coverage: 91.625% (+0.004%) from 91.621%
858f5658-b835-4f01-86b4-3a69cebd0b86

push

circleci

Mirella de Medeiros
Bump version: 1.0.1 → 1.1.0

1 of 1 new or added line in 1 file covered. (100.0%)

19988 of 21815 relevant lines covered (91.63%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.58
/boa3/internal/model/builtin/method/maxmethod.py
1
from typing import Any, Dict, Optional
1✔
2

3
from boa3.internal.model.builtin.method.builtinmethod import IBuiltinMethod
1✔
4
from boa3.internal.model.expression import IExpression
1✔
5
from boa3.internal.model.type.collection.sequence.sequencetype import SequenceType
1✔
6
from boa3.internal.model.type.collection.sequence.tupletype import TupleType
1✔
7
from boa3.internal.model.type.itype import IType
1✔
8
from boa3.internal.model.variable import Variable
1✔
9
from boa3.internal.neo.vm.opcode.Opcode import Opcode
1✔
10

11

12
class MaxMethod(IBuiltinMethod):
1✔
13

14
    def __init__(self, arg_value: Optional[IType] = None):
1✔
15
        from boa3.internal.model.type.type import Type
1✔
16
        identifier = 'max'
1✔
17

18
        self._allowed_types = [Type.int, Type.str, Type.bytes]
1✔
19
        default_type = Type.int
1✔
20
        if not self._is_valid_type(arg_value):
1✔
21
            arg_value = default_type
×
22

23
        args: Dict[str, Variable] = {
1✔
24
            'args1': Variable(arg_value),
25
            'args2': Variable(arg_value)
26
        }
27
        vararg = ('values', Variable(arg_value))
1✔
28
        super().__init__(identifier, args, return_type=arg_value, vararg=vararg)
1✔
29

30
    def _is_valid_type(self, arg_type: Optional[IType]) -> bool:
1✔
31
        return (isinstance(arg_type, IType) and
1✔
32
                any(allowed_type.is_type_of(arg_type) for allowed_type in self._allowed_types))
33

34
    @property
1✔
35
    def identifier(self) -> str:
1✔
36
        from boa3.internal.model.type.type import Type
1✔
37
        if self._arg_values.type is Type.int:
1✔
38
            return self._identifier
1✔
39
        return '-{0}_from_{1}'.format(self._identifier, self._arg_values.type._identifier)
1✔
40

41
    @property
1✔
42
    def _arg_values(self) -> Variable:
1✔
43
        return self._vararg[1]
1✔
44

45
    def validate_parameters(self, *params: IExpression) -> bool:
1✔
46
        if len(params) != 1:
×
47
            return False
×
48
        if not isinstance(params[0], IExpression):
×
49
            return False
×
50
        return isinstance(params[0].type, SequenceType)
×
51

52
    def generate_internal_opcodes(self, code_generator):
1✔
53
        code_generator.insert_opcode(Opcode.MAX)
1✔
54

55
    def generate_opcodes(self, code_generator):
1✔
56
        from boa3.internal.model.builtin.builtin import Builtin
1✔
57
        from boa3.internal.model.type.type import Type
1✔
58

59
        # if len(stack) == 2:
60
        code_generator.insert_opcode(Opcode.DEPTH, add_to_stack=[Type.int])
1✔
61
        code_generator.convert_literal(2)
1✔
62
        if_stack_size_equals_2 = code_generator.convert_begin_if()
1✔
63
        code_generator.change_jump(if_stack_size_equals_2, Opcode.JMPNE)
1✔
64

65
        #   aux_list = [arg1, arg2]
66
        code_generator.convert_literal(2)
1✔
67

68
        # else:
69
        else_stack_size_equals_3 = code_generator.convert_begin_else(if_stack_size_equals_2, is_internal=True)
1✔
70
        #   aux_list = [arg1, arg2, *values]
71
        code_generator.swap_reverse_stack_items(3)
1✔
72
        code_generator.insert_opcode(Opcode.UNPACK)
1✔
73
        code_generator.insert_opcode(Opcode.INC)
1✔
74
        code_generator.insert_opcode(Opcode.INC)
1✔
75
        code_generator.convert_end_if(else_stack_size_equals_3, is_internal=True)
1✔
76
        code_generator.insert_opcode(Opcode.PACK)
1✔
77

78
        # index = len(aux_list) - 1
79
        code_generator.duplicate_stack_top_item()
1✔
80
        code_generator.convert_builtin_method_call(Builtin.Len, is_internal=True)
1✔
81
        code_generator.insert_opcode(Opcode.DEC)
1✔
82
        code_generator.duplicate_stack_item(2)
1✔
83
        code_generator.duplicate_stack_item(2)
1✔
84
        # current_max = aux_list[index]
85
        code_generator.convert_get_item(index_inserted_internally=True, test_is_negative_index=False)
1✔
86

87
        # while (index != 0):
88
        start_while = code_generator.convert_begin_while()
1✔
89

90
        #   index -= 1
91
        code_generator.swap_reverse_stack_items(2)
1✔
92
        code_generator.insert_opcode(Opcode.DEC)
1✔
93
        code_generator.swap_reverse_stack_items(2)
1✔
94
        code_generator.duplicate_stack_item(3)
1✔
95
        code_generator.duplicate_stack_item(3)
1✔
96
        code_generator.convert_get_item(index_inserted_internally=True, test_is_negative_index=False)
1✔
97

98
        #   current_max = max(current_max, aux_list[index])
99
        self._compare_values(code_generator)
1✔
100

101
        # while condition and end
102
        condition_address = code_generator.bytecode_size
1✔
103
        code_generator.duplicate_stack_item(2)
1✔
104
        code_generator.insert_opcode(Opcode.SIGN)
1✔
105
        code_generator.convert_end_while(start_while, condition_address, is_internal=True)
1✔
106

107
        # return max
108
        code_generator.swap_reverse_stack_items(3)
1✔
109
        code_generator.remove_stack_top_item()
1✔
110
        code_generator.remove_stack_top_item()
1✔
111

112
    def _compare_values(self, code_generator):
1✔
113
        """
114
        :type code_generator: boa3.internal.compiler.codegenerator.codegenerator.CodeGenerator
115
        """
116
        self.generate_internal_opcodes(code_generator)
1✔
117

118
    @property
1✔
119
    def _args_on_stack(self) -> int:
1✔
120
        return len(self.args)
1✔
121

122
    @property
1✔
123
    def _body(self) -> Optional[str]:
1✔
124
        return
×
125

126
    def build(self, value: Any) -> IBuiltinMethod:
1✔
127
        if isinstance(value, list) and len(value) > 0:
1✔
128
            value = value[0]
1✔
129
        if isinstance(value, TupleType):
1✔
130
            value = value.value_type
×
131
        if type(value) == type(self._arg_values.type):
1✔
132
            return self
1✔
133

134
        from boa3.internal.model.builtin.method.maxbytestringmethod import MaxByteStringMethod
1✔
135
        from boa3.internal.model.builtin.method.maxintmethod import MaxIntMethod
1✔
136
        from boa3.internal.model.type.type import Type
1✔
137

138
        if Type.str.is_type_of(value) or Type.bytes.is_type_of(value):
1✔
139
            return MaxByteStringMethod(value)
1✔
140

141
        return MaxIntMethod(value)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc