Skip to content

Commit 614702b

Browse files
committed
add rust test for realloc example
1 parent f3e8e8d commit 614702b

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

basics/realloc/native/program/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ solana-system-interface.workspace = true
1313
crate-type = ["cdylib", "lib"]
1414

1515
[features]
16-
anchor-debug = []
1716
custom-heap = []
1817
custom-panic = []
1918

2019
[lints.rust]
2120
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }
21+
22+
[dev-dependencies]
23+
litesvm = "0.8.1"
24+
solana-instruction = "3.0.0"
25+
solana-keypair = "3.0.1"
26+
solana-native-token = "3.0.0"
27+
solana-pubkey = "3.0.0"
28+
solana-transaction = "3.0.1"
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
use litesvm::LiteSVM;
2+
use realloc_program::state::{AddressInfo, WorkInfo};
3+
use realloc_program::{processor::ReallocInstruction, state::EnhancedAddressInfoExtender};
4+
use solana_instruction::Instruction;
5+
use solana_keypair::{Keypair, Signer};
6+
use solana_native_token::LAMPORTS_PER_SOL;
7+
use solana_pubkey::Pubkey;
8+
use solana_transaction::{AccountMeta, Transaction};
9+
10+
#[test]
11+
fn test_realloc() {
12+
let mut svm = LiteSVM::new();
13+
14+
let program_id = Pubkey::new_unique();
15+
let program_bytes = include_bytes!("../../../../../target/deploy/realloc_program.so");
16+
17+
svm.add_program(program_id, program_bytes).unwrap();
18+
19+
let payer = Keypair::new();
20+
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL).unwrap();
21+
22+
let test_account = Keypair::new();
23+
24+
let address_info = AddressInfo {
25+
name: "Jacob".to_string(),
26+
house_number: 123,
27+
street: "Main St.".to_string(),
28+
city: "Chicago".to_string(),
29+
};
30+
31+
let data = borsh::to_vec(&ReallocInstruction::Create(address_info)).unwrap();
32+
33+
let ix = Instruction {
34+
program_id,
35+
accounts: vec![
36+
AccountMeta::new(test_account.pubkey(), true),
37+
AccountMeta::new(payer.pubkey(), true),
38+
AccountMeta::new(solana_system_interface::program::ID, false),
39+
],
40+
data,
41+
};
42+
43+
let tx = Transaction::new_signed_with_payer(
44+
&[ix],
45+
Some(&payer.pubkey()),
46+
&[&payer, &test_account],
47+
svm.latest_blockhash(),
48+
);
49+
50+
let _ = svm.send_transaction(tx).is_ok();
51+
52+
let data = borsh::to_vec(&ReallocInstruction::ReallocateWithoutZeroInit(
53+
EnhancedAddressInfoExtender {
54+
state: "Illinois".to_string(),
55+
zip: 12345,
56+
},
57+
))
58+
.unwrap();
59+
60+
let ix = Instruction {
61+
program_id,
62+
accounts: vec![
63+
AccountMeta::new(test_account.pubkey(), false),
64+
AccountMeta::new(payer.pubkey(), true),
65+
AccountMeta::new(solana_system_interface::program::ID, false),
66+
],
67+
data,
68+
};
69+
70+
let tx = Transaction::new_signed_with_payer(
71+
&[ix],
72+
Some(&payer.pubkey()),
73+
&[&payer],
74+
svm.latest_blockhash(),
75+
);
76+
77+
let _ = svm.send_transaction(tx).is_ok();
78+
79+
let data = borsh::to_vec(&ReallocInstruction::ReallocateZeroInit(WorkInfo {
80+
name: "Pete".to_string(),
81+
position: "Engineer".to_string(),
82+
company: "Solana Labs".to_string(),
83+
years_employed: 2,
84+
}))
85+
.unwrap();
86+
87+
let ix = Instruction {
88+
program_id,
89+
accounts: vec![AccountMeta::new(test_account.pubkey(), false)],
90+
data,
91+
};
92+
93+
let tx = Transaction::new_signed_with_payer(
94+
&[ix],
95+
Some(&payer.pubkey()),
96+
&[&payer],
97+
svm.latest_blockhash(),
98+
);
99+
100+
let _ = svm.send_transaction(tx).is_ok();
101+
}

0 commit comments

Comments
 (0)