@@ -27,6 +27,7 @@ use crate::vm::analysis::AnalysisDatabase;
27
27
use crate :: vm:: ast:: errors:: ParseErrors ;
28
28
use crate :: vm:: ast:: { build_ast, parse} ;
29
29
use crate :: vm:: contexts:: OwnedEnvironment ;
30
+ use crate :: vm:: execute_v2;
30
31
use crate :: vm:: representations:: SymbolicExpression ;
31
32
use crate :: vm:: types:: {
32
33
BufferLength , FixedFunction , FunctionType , PrincipalData , QualifiedContractIdentifier ,
@@ -74,6 +75,145 @@ fn ascii_type(size: u32) -> TypeSignature {
74
75
TypeSignature :: SequenceType ( StringType ( ASCII ( size. try_into ( ) . unwrap ( ) ) ) ) . into ( )
75
76
}
76
77
78
+ #[ test]
79
+ fn test_from_consensus_buff ( ) {
80
+ let good = [
81
+ ( "(from-consensus-buff int 0x00)" , "(optional int)" ) ,
82
+ (
83
+ "(from-consensus-buff { a: uint, b: principal } 0x00)" ,
84
+ "(optional (tuple (a uint) (b principal)))" ,
85
+ ) ,
86
+ ] ;
87
+
88
+ let bad = [
89
+ (
90
+ "(from-consensus-buff)" ,
91
+ CheckErrors :: IncorrectArgumentCount ( 2 , 0 ) ,
92
+ ) ,
93
+ (
94
+ "(from-consensus-buff 0x00 0x00 0x00)" ,
95
+ CheckErrors :: IncorrectArgumentCount ( 2 , 3 ) ,
96
+ ) ,
97
+ (
98
+ "(from-consensus-buff 0x00 0x00)" ,
99
+ CheckErrors :: InvalidTypeDescription ,
100
+ ) ,
101
+ (
102
+ "(from-consensus-buff int u6)" ,
103
+ CheckErrors :: TypeError ( TypeSignature :: max_buffer ( ) , TypeSignature :: UIntType ) ,
104
+ ) ,
105
+ (
106
+ "(from-consensus-buff (buff 1048576) 0x00)" ,
107
+ CheckErrors :: ValueTooLarge ,
108
+ ) ,
109
+ ] ;
110
+
111
+ for ( good_test, expected) in good. iter ( ) {
112
+ let type_result = type_check_helper ( good_test) . unwrap ( ) ;
113
+ assert_eq ! ( expected, & type_result. to_string( ) ) ;
114
+
115
+ assert ! (
116
+ type_result. admits( & execute_v2( good_test) . unwrap( ) . unwrap( ) ) ,
117
+ "The analyzed type must admit the evaluated type"
118
+ ) ;
119
+ }
120
+
121
+ for ( bad_test, expected) in bad. iter ( ) {
122
+ assert_eq ! ( expected, & type_check_helper( & bad_test) . unwrap_err( ) . err) ;
123
+ }
124
+ }
125
+
126
+ #[ test]
127
+ fn test_to_consensus_buff ( ) {
128
+ let good = [
129
+ (
130
+ "(to-consensus-buff (if true (some u1) (some u2)))" ,
131
+ "(optional (buff 18))" ,
132
+ ) ,
133
+ (
134
+ "(to-consensus-buff (if true (ok u1) (ok u2)))" ,
135
+ "(optional (buff 18))" ,
136
+ ) ,
137
+ (
138
+ "(to-consensus-buff (if true (ok 1) (err u2)))" ,
139
+ "(optional (buff 18))" ,
140
+ ) ,
141
+ (
142
+ "(to-consensus-buff (if true (ok 1) (err true)))" ,
143
+ "(optional (buff 18))" ,
144
+ ) ,
145
+ (
146
+ "(to-consensus-buff (if true (ok false) (err true)))" ,
147
+ "(optional (buff 2))" ,
148
+ ) ,
149
+ (
150
+ "(to-consensus-buff (if true (err u1) (err u2)))" ,
151
+ "(optional (buff 18))" ,
152
+ ) ,
153
+ ( "(to-consensus-buff none)" , "(optional (buff 1))" ) ,
154
+ ( "(to-consensus-buff 0x00)" , "(optional (buff 6))" ) ,
155
+ ( "(to-consensus-buff \" a\" )" , "(optional (buff 6))" ) ,
156
+ ( "(to-consensus-buff u\" ab\" )" , "(optional (buff 13))" ) ,
157
+ ( "(to-consensus-buff 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6)" , "(optional (buff 151))" ) ,
158
+ ( "(to-consensus-buff 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6.abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde)" , "(optional (buff 151))" ) ,
159
+ ( "(to-consensus-buff true)" , "(optional (buff 1))" ) ,
160
+ ( "(to-consensus-buff -1)" , "(optional (buff 17))" ) ,
161
+ ( "(to-consensus-buff u1)" , "(optional (buff 17))" ) ,
162
+ ( "(to-consensus-buff (list 1 2 3 4))" , "(optional (buff 73))" ) ,
163
+ (
164
+ "(to-consensus-buff { apple: u1, orange: 2, blue: true })" ,
165
+ "(optional (buff 58))" ,
166
+ ) ,
167
+ (
168
+ "(define-private (my-func (x (buff 1048566)))
169
+ (to-consensus-buff x))
170
+ (my-func 0x001122334455)
171
+ " ,
172
+ "(optional (buff 1048571))" ,
173
+ ) ,
174
+ ] ;
175
+
176
+ let bad = [
177
+ (
178
+ "(to-consensus-buff)" ,
179
+ CheckErrors :: IncorrectArgumentCount ( 1 , 0 ) ,
180
+ ) ,
181
+ (
182
+ "(to-consensus-buff 0x00 0x00)" ,
183
+ CheckErrors :: IncorrectArgumentCount ( 1 , 2 ) ,
184
+ ) ,
185
+ (
186
+ "(define-private (my-func (x (buff 1048576)))
187
+ (to-consensus-buff x))" ,
188
+ CheckErrors :: ValueTooLarge ,
189
+ ) ,
190
+ (
191
+ "(define-private (my-func (x (buff 1048570)))
192
+ (to-consensus-buff x))" ,
193
+ CheckErrors :: ValueTooLarge ,
194
+ ) ,
195
+ (
196
+ "(define-private (my-func (x (buff 1048567)))
197
+ (to-consensus-buff x))" ,
198
+ CheckErrors :: ValueTooLarge ,
199
+ ) ,
200
+ ] ;
201
+
202
+ for ( good_test, expected) in good. iter ( ) {
203
+ let type_result = type_check_helper ( good_test) . unwrap ( ) ;
204
+ assert_eq ! ( expected, & type_result. to_string( ) ) ;
205
+
206
+ assert ! (
207
+ type_result. admits( & execute_v2( good_test) . unwrap( ) . unwrap( ) ) ,
208
+ "The analyzed type must admit the evaluated type"
209
+ ) ;
210
+ }
211
+
212
+ for ( bad_test, expected) in bad. iter ( ) {
213
+ assert_eq ! ( expected, & type_check_helper( & bad_test) . unwrap_err( ) . err) ;
214
+ }
215
+ }
216
+
77
217
#[ test]
78
218
fn test_get_block_info ( ) {
79
219
let good = [
0 commit comments