space.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. use anchor_lang::prelude::*;
  2. // Needed to declare accounts.
  3. declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
  4. mod inside_mod {
  5. use super::*;
  6. #[derive(InitSpace)]
  7. pub struct Data {
  8. pub data: u64,
  9. }
  10. }
  11. #[derive(InitSpace)]
  12. pub enum TestBasicEnum {
  13. Basic1,
  14. Basic2 {
  15. test_u8: u8,
  16. },
  17. Basic3 {
  18. test_u16: u16,
  19. },
  20. Basic4 {
  21. #[max_len(10)]
  22. test_vec: Vec<u8>,
  23. },
  24. }
  25. #[account]
  26. #[derive(InitSpace)]
  27. pub struct TestEmptyAccount {}
  28. #[account]
  29. #[derive(InitSpace)]
  30. pub struct TestBasicVarAccount {
  31. pub test_u8: u8,
  32. pub test_u16: u16,
  33. pub test_u32: u32,
  34. pub test_u64: u64,
  35. pub test_u128: u128,
  36. }
  37. #[account]
  38. #[derive(InitSpace)]
  39. pub struct TestComplexVarAccount {
  40. pub test_key: Pubkey,
  41. #[max_len(10)]
  42. pub test_vec: Vec<u8>,
  43. #[max_len(10)]
  44. pub test_string: String,
  45. pub test_option: Option<u16>,
  46. }
  47. #[derive(InitSpace)]
  48. pub struct TestNonAccountStruct {
  49. pub test_bool: bool,
  50. }
  51. #[account(zero_copy)]
  52. #[derive(InitSpace)]
  53. pub struct TestZeroCopyStruct {
  54. pub test_array: [u8; 8],
  55. pub test_u32: u32,
  56. }
  57. #[derive(InitSpace)]
  58. pub struct ChildStruct {
  59. #[max_len(10)]
  60. pub test_string: String,
  61. }
  62. #[derive(InitSpace)]
  63. pub struct TestNestedStruct {
  64. pub test_struct: ChildStruct,
  65. pub test_enum: TestBasicEnum,
  66. }
  67. #[derive(InitSpace)]
  68. pub struct TestMatrixStruct {
  69. #[max_len(2, 4)]
  70. pub test_matrix: Vec<Vec<u8>>,
  71. }
  72. #[derive(InitSpace)]
  73. pub struct TestFullPath {
  74. pub test_option_path: Option<inside_mod::Data>,
  75. pub test_path: inside_mod::Data,
  76. }
  77. const MAX_LEN: u8 = 10;
  78. #[derive(InitSpace)]
  79. pub struct TestConst {
  80. #[max_len(MAX_LEN)]
  81. pub test_string: String,
  82. pub test_array: [u8; MAX_LEN as usize],
  83. }
  84. #[derive(InitSpace)]
  85. pub struct TestUnnamedStruct(
  86. pub u8,
  87. #[max_len(4)] pub Vec<u32>,
  88. #[max_len(10)] pub String,
  89. pub ChildStruct,
  90. pub TestBasicEnum,
  91. );
  92. #[derive(InitSpace)]
  93. pub struct TestUnitStruct;
  94. #[derive(InitSpace)]
  95. #[allow(clippy::type_complexity)]
  96. pub struct TestTupleStruct {
  97. pub test_tuple: (u8, u16, u32, u64, u128),
  98. pub mixed_tuple: (bool, f32, f64, i8, i16, i32, i64, i128),
  99. pub nested_tuple: (u8, (u16, u32, u64, u128)),
  100. pub deeply_nested: (u8, (u16, (u32, (u64, u128)))),
  101. pub complex_nested: (bool, (u8, u16), (u32, (u64, u128))),
  102. pub option_tuple: Option<(u8, u16, u32, u64, u128)>,
  103. pub tuple_with_option: (u8, Option<u16>, u32),
  104. pub nested_option_tuple: (u8, Option<(u16, u32)>, u64),
  105. pub pubkey_tuple: (Pubkey, u64),
  106. pub tuple_with_pubkeys: (Pubkey, Pubkey, u8),
  107. pub struct_tuple: (ChildStruct, u8),
  108. pub nested_struct_tuple: (u8, (ChildStruct, u16)),
  109. pub single_tuple: (u64,),
  110. pub single_nested: ((u8,),),
  111. pub empty_tuple: (),
  112. pub tuple_with_empty: (u8, (), u16),
  113. pub array_tuple: ([u8; 4], u16),
  114. pub tuple_array_nested: (u8, ([u16; 2], u32)),
  115. pub ultimate_complex: (u8, (bool, Option<(u16, u32)>, ChildStruct), Pubkey),
  116. }
  117. #[test]
  118. fn test_empty_struct() {
  119. assert_eq!(TestEmptyAccount::INIT_SPACE, 0);
  120. }
  121. #[test]
  122. fn test_basic_struct() {
  123. assert_eq!(TestBasicVarAccount::INIT_SPACE, 1 + 2 + 4 + 8 + 16);
  124. }
  125. #[test]
  126. fn test_complex_struct() {
  127. assert_eq!(
  128. TestComplexVarAccount::INIT_SPACE,
  129. 32 + 4 + 10 + (4 + 10) + 3
  130. )
  131. }
  132. #[test]
  133. fn test_zero_copy_struct() {
  134. assert_eq!(TestZeroCopyStruct::INIT_SPACE, 8 + 4)
  135. }
  136. #[test]
  137. fn test_basic_enum() {
  138. assert_eq!(TestBasicEnum::INIT_SPACE, 1 + 14);
  139. }
  140. #[test]
  141. fn test_nested_struct() {
  142. assert_eq!(
  143. TestNestedStruct::INIT_SPACE,
  144. ChildStruct::INIT_SPACE + TestBasicEnum::INIT_SPACE
  145. )
  146. }
  147. #[test]
  148. fn test_matrix_struct() {
  149. assert_eq!(TestMatrixStruct::INIT_SPACE, 4 + (2 * (4 + 4)))
  150. }
  151. #[test]
  152. fn test_full_path() {
  153. assert_eq!(TestFullPath::INIT_SPACE, 8 + 9)
  154. }
  155. #[test]
  156. fn test_const() {
  157. assert_eq!(TestConst::INIT_SPACE, (4 + 10) + 10)
  158. }
  159. #[test]
  160. fn test_unnamed_struct() {
  161. assert_eq!(
  162. TestUnnamedStruct::INIT_SPACE,
  163. 1 + 4 + 4 * 4 + 4 + 10 + ChildStruct::INIT_SPACE + TestBasicEnum::INIT_SPACE
  164. )
  165. }
  166. #[test]
  167. fn test_unit_struct() {
  168. assert_eq!(TestUnitStruct::INIT_SPACE, 0)
  169. }
  170. #[test]
  171. fn test_basic_tuple() {
  172. let basic_tuple_size = 1 + 2 + 4 + 8 + 16; // 31
  173. assert!(TestTupleStruct::INIT_SPACE >= basic_tuple_size);
  174. }
  175. #[test]
  176. fn test_tuple_space_calculations() {
  177. let basic_tuple_size = 1 + 2 + 4 + 8 + 16; // 31
  178. let mixed_tuple_size = 1 + 4 + 8 + 1 + 2 + 4 + 8 + 16; // 44
  179. let nested_tuple_size = 1 + (2 + 4 + 8 + 16); // 31
  180. let option_tuple_size = 1 + (1 + 2 + 4 + 8 + 16); // 32
  181. let pubkey_tuple_size = 32 + 8; // 40
  182. let single_tuple_size = 8;
  183. let empty_tuple_size = 0;
  184. let minimum_expected_size = basic_tuple_size
  185. + mixed_tuple_size
  186. + nested_tuple_size
  187. + option_tuple_size
  188. + pubkey_tuple_size
  189. + single_tuple_size
  190. + empty_tuple_size;
  191. assert!(TestTupleStruct::INIT_SPACE >= minimum_expected_size);
  192. }
  193. #[test]
  194. fn test_tuple_with_structs() {
  195. // Test that tuples containing other structs work correctly
  196. // struct_tuple: (ChildStruct, u8) = ChildStruct::INIT_SPACE + 1
  197. let expected_struct_tuple_contribution = ChildStruct::INIT_SPACE + 1;
  198. assert!(TestTupleStruct::INIT_SPACE >= expected_struct_tuple_contribution);
  199. }
  200. #[test]
  201. fn test_nested_tuple_complexity() {
  202. // Test deeply_nested: (u8, (u16, (u32, (u64, u128))))
  203. // = 1 + (2 + (4 + (8 + 16))) = 1 + (2 + (4 + 24)) = 1 + (2 + 28) = 1 + 30 = 31
  204. let deeply_nested_size = 1 + 2 + 4 + 8 + 16; // 31
  205. // Test complex_nested: (bool, (u8, u16), (u32, (u64, u128)))
  206. // = 1 + (1 + 2) + (4 + (8 + 16)) = 1 + 3 + (4 + 24) = 1 + 3 + 28 = 32
  207. let complex_nested_size = 1 + (1 + 2) + (4 + (8 + 16)); // 32
  208. assert!(TestTupleStruct::INIT_SPACE >= deeply_nested_size + complex_nested_size);
  209. }
  210. #[test]
  211. fn test_tuple_with_options() {
  212. // tuple_with_option: (u8, Option<u16>, u32) = 1 + (1 + 2) + 4 = 8
  213. let tuple_with_option_size = 1 + (1 + 2) + 4; // 8
  214. // nested_option_tuple: (u8, Option<(u16, u32)>, u64) = 1 + (1 + (2 + 4)) + 8 = 16
  215. let nested_option_tuple_size = 1 + (1 + (2 + 4)) + 8; // 16
  216. assert!(TestTupleStruct::INIT_SPACE >= tuple_with_option_size + nested_option_tuple_size);
  217. }
  218. #[test]
  219. fn test_tuple_with_arrays() {
  220. // array_tuple: ([u8; 4], u16) = (4 * 1) + 2 = 6
  221. let array_tuple_size = 4 + 2; // 6
  222. // tuple_array_nested: (u8, ([u16; 2], u32)) = 1 + ((2 * 2) + 4) = 1 + (4 + 4) = 9
  223. let tuple_array_nested_size = 1 + ((2 * 2) + 4); // 9
  224. assert!(TestTupleStruct::INIT_SPACE >= array_tuple_size + tuple_array_nested_size);
  225. }