-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathstrings.rs
135 lines (123 loc) · 3.33 KB
/
strings.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: Apache-2.0
use crate::{build_solidity, BorshToken};
use num_bigint::BigInt;
use num_traits::{One, Zero};
#[test]
fn storage_string_length() {
let mut vm = build_solidity(
r#"
contract Testing {
string st;
function setString(string input) public {
st = input;
}
function getLength() public view returns (uint32) {
return st.length;
}
}
"#,
);
let data_account = vm.initialize_data_account();
vm.function("new")
.accounts(vec![("dataAccount", data_account)])
.call();
let _ = vm
.function("setString")
.arguments(&[BorshToken::String("coffee_tastes_good".to_string())])
.accounts(vec![("dataAccount", data_account)])
.call();
let returns = vm
.function("getLength")
.accounts(vec![("dataAccount", data_account)])
.call()
.unwrap();
assert_eq!(
returns,
BorshToken::Uint {
width: 32,
value: BigInt::from(18u8),
}
);
}
#[test]
fn load_string_vector() {
let mut vm = build_solidity(
r#"
contract Testing {
string[] string_vec;
function testLength() public returns (uint32, uint32, uint32) {
string_vec.push("tea");
string_vec.push("coffe");
string_vec.push("sixsix");
string[] memory rr = string_vec;
return (rr[0].length, rr[1].length, rr[2].length);
}
function getString(uint32 index) public view returns (string memory) {
string[] memory rr = string_vec;
return rr[index];
}
}
"#,
);
let data_account = vm.initialize_data_account();
vm.function("new")
.accounts(vec![("dataAccount", data_account)])
.call();
let returns = vm
.function("testLength")
.accounts(vec![("dataAccount", data_account)])
.call()
.unwrap()
.unwrap_tuple();
assert_eq!(
returns[0],
BorshToken::Uint {
width: 32,
value: BigInt::from(3u8),
}
);
assert_eq!(
returns[1],
BorshToken::Uint {
width: 32,
value: BigInt::from(5u8),
}
);
assert_eq!(
returns[2],
BorshToken::Uint {
width: 32,
value: BigInt::from(6u8),
}
);
let returns = vm
.function("getString")
.arguments(&[BorshToken::Uint {
width: 32,
value: BigInt::zero(),
}])
.accounts(vec![("dataAccount", data_account)])
.call()
.unwrap();
assert_eq!(returns, BorshToken::String("tea".to_string()));
let returns = vm
.function("getString")
.arguments(&[BorshToken::Uint {
width: 32,
value: BigInt::one(),
}])
.accounts(vec![("dataAccount", data_account)])
.call()
.unwrap();
assert_eq!(returns, BorshToken::String("coffe".to_string()));
let returns = vm
.function("getString")
.arguments(&[BorshToken::Uint {
width: 32,
value: BigInt::from(2u8),
}])
.accounts(vec![("dataAccount", data_account)])
.call()
.unwrap();
assert_eq!(returns, BorshToken::String("sixsix".to_string()));
}