space.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 TestComplexeVarAccount {
  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. #[test]
  85. fn test_empty_struct() {
  86. assert_eq!(TestEmptyAccount::INIT_SPACE, 0);
  87. }
  88. #[test]
  89. fn test_basic_struct() {
  90. assert_eq!(TestBasicVarAccount::INIT_SPACE, 1 + 2 + 4 + 8 + 16);
  91. }
  92. #[test]
  93. fn test_complexe_struct() {
  94. assert_eq!(
  95. TestComplexeVarAccount::INIT_SPACE,
  96. 32 + 4 + 10 + (4 + 10) + 3
  97. )
  98. }
  99. #[test]
  100. fn test_zero_copy_struct() {
  101. assert_eq!(TestZeroCopyStruct::INIT_SPACE, 8 + 4)
  102. }
  103. #[test]
  104. fn test_basic_enum() {
  105. assert_eq!(TestBasicEnum::INIT_SPACE, 1 + 14);
  106. }
  107. #[test]
  108. fn test_nested_struct() {
  109. assert_eq!(
  110. TestNestedStruct::INIT_SPACE,
  111. ChildStruct::INIT_SPACE + TestBasicEnum::INIT_SPACE
  112. )
  113. }
  114. #[test]
  115. fn test_matrix_struct() {
  116. assert_eq!(TestMatrixStruct::INIT_SPACE, 4 + (2 * (4 + 4)))
  117. }
  118. #[test]
  119. fn test_full_path() {
  120. assert_eq!(TestFullPath::INIT_SPACE, 8 + 9)
  121. }
  122. #[test]
  123. fn test_const() {
  124. assert_eq!(TestConst::INIT_SPACE, (4 + 10) + 10)
  125. }