space.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #[test]
  95. fn test_empty_struct() {
  96. assert_eq!(TestEmptyAccount::INIT_SPACE, 0);
  97. }
  98. #[test]
  99. fn test_basic_struct() {
  100. assert_eq!(TestBasicVarAccount::INIT_SPACE, 1 + 2 + 4 + 8 + 16);
  101. }
  102. #[test]
  103. fn test_complex_struct() {
  104. assert_eq!(
  105. TestComplexVarAccount::INIT_SPACE,
  106. 32 + 4 + 10 + (4 + 10) + 3
  107. )
  108. }
  109. #[test]
  110. fn test_zero_copy_struct() {
  111. assert_eq!(TestZeroCopyStruct::INIT_SPACE, 8 + 4)
  112. }
  113. #[test]
  114. fn test_basic_enum() {
  115. assert_eq!(TestBasicEnum::INIT_SPACE, 1 + 14);
  116. }
  117. #[test]
  118. fn test_nested_struct() {
  119. assert_eq!(
  120. TestNestedStruct::INIT_SPACE,
  121. ChildStruct::INIT_SPACE + TestBasicEnum::INIT_SPACE
  122. )
  123. }
  124. #[test]
  125. fn test_matrix_struct() {
  126. assert_eq!(TestMatrixStruct::INIT_SPACE, 4 + (2 * (4 + 4)))
  127. }
  128. #[test]
  129. fn test_full_path() {
  130. assert_eq!(TestFullPath::INIT_SPACE, 8 + 9)
  131. }
  132. #[test]
  133. fn test_const() {
  134. assert_eq!(TestConst::INIT_SPACE, (4 + 10) + 10)
  135. }
  136. #[test]
  137. fn test_unnamed_struct() {
  138. assert_eq!(
  139. TestUnnamedStruct::INIT_SPACE,
  140. 1 + 4 + 4 * 4 + 4 + 10 + ChildStruct::INIT_SPACE + TestBasicEnum::INIT_SPACE
  141. )
  142. }
  143. #[test]
  144. fn test_unit_struct() {
  145. assert_eq!(TestUnitStruct::INIT_SPACE, 0)
  146. }