|
| 1 | +contract; |
| 2 | + |
| 3 | +use core::experimental::storage::*; |
| 4 | +use std::experimental::storage::*; |
| 5 | + |
| 6 | +struct M { |
| 7 | + u: b256, |
| 8 | + v: u64, |
| 9 | +} |
| 10 | + |
| 11 | +impl core::ops::Eq for M { |
| 12 | + fn eq(self, other: Self) -> bool { |
| 13 | + self.u == other.u && self.v == other.v |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +pub enum E { |
| 18 | + A: u64, |
| 19 | + B: b256, |
| 20 | +} |
| 21 | + |
| 22 | +impl core::ops::Eq for E { |
| 23 | + fn eq(self, other: Self) -> bool { |
| 24 | + match (self, other) { |
| 25 | + (E::A(l), E::A(r)) => l == r, |
| 26 | + (E::B(l), E::B(r)) => l == r, |
| 27 | + _ => false, |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +storage { |
| 33 | + nested_map_1: StorageMap<u64, StorageMap<u64, StorageMap<u64, u64>>> = StorageMap {}, |
| 34 | + nested_map_2: StorageMap<(u64, u64), StorageMap<str[4], StorageMap<u64, M>>> = StorageMap {}, |
| 35 | + nested_map_3: StorageMap<u64, StorageMap<M, StorageMap<u64, E>>> = StorageMap {}, |
| 36 | +} |
| 37 | + |
| 38 | +abi ExperimentalStorageTest { |
| 39 | + #[storage(read, write)] |
| 40 | + fn nested_map_1_access(); |
| 41 | + |
| 42 | + #[storage(read, write)] |
| 43 | + fn nested_map_2_access(); |
| 44 | + |
| 45 | + #[storage(read, write)] |
| 46 | + fn nested_map_3_access(); |
| 47 | +} |
| 48 | + |
| 49 | +impl ExperimentalStorageTest for Contract { |
| 50 | + #[storage(read, write)] |
| 51 | + fn nested_map_1_access() { |
| 52 | + storage.nested_map_1.get_handle(0).get_handle(0).insert(0, 1); |
| 53 | + storage.nested_map_1.get_handle(0).get_handle(0).insert(1, 2); |
| 54 | + storage.nested_map_1.get_handle(0).get_handle(1).insert(0, 3); |
| 55 | + storage.nested_map_1.get_handle(0).get_handle(1).insert(1, 4); |
| 56 | + storage.nested_map_1.get_handle(1).get_handle(0).insert(0, 5); |
| 57 | + storage.nested_map_1.get_handle(1).get_handle(0).insert(1, 6); |
| 58 | + storage.nested_map_1.get_handle(1).get_handle(1).insert(0, 7); |
| 59 | + storage.nested_map_1.get_handle(1).get_handle(1).insert(1, 8); |
| 60 | + |
| 61 | + assert(storage.nested_map_1.get_handle(0).get_handle(0).get(0).unwrap() == 1); |
| 62 | + assert(storage.nested_map_1.get_handle(0).get_handle(0).get(1).unwrap() == 2); |
| 63 | + assert(storage.nested_map_1.get_handle(0).get_handle(1).get(0).unwrap() == 3); |
| 64 | + assert(storage.nested_map_1.get_handle(0).get_handle(1).get(1).unwrap() == 4); |
| 65 | + assert(storage.nested_map_1.get_handle(1).get_handle(0).get(0).unwrap() == 5); |
| 66 | + assert(storage.nested_map_1.get_handle(1).get_handle(0).get(1).unwrap() == 6); |
| 67 | + assert(storage.nested_map_1.get_handle(1).get_handle(1).get(0).unwrap() == 7); |
| 68 | + assert(storage.nested_map_1.get_handle(1).get_handle(1).get(1).unwrap() == 8); |
| 69 | + |
| 70 | + assert(storage.nested_map_1.get_handle(2).get_handle(1).get(1).is_none()); |
| 71 | + assert(storage.nested_map_1.get_handle(1).get_handle(2).get(1).is_none()); |
| 72 | + assert(storage.nested_map_1.get_handle(1).get_handle(1).get(2).is_none()); |
| 73 | + } |
| 74 | + |
| 75 | + #[storage(read, write)] |
| 76 | + fn nested_map_2_access() { |
| 77 | + let m1 = M { |
| 78 | + u: 0x1111111111111111111111111111111111111111111111111111111111111111, |
| 79 | + v: 1 |
| 80 | + }; |
| 81 | + let m2 = M { |
| 82 | + u: 0x2222222222222222222222222222222222222222222222222222222222222222, |
| 83 | + v: 2 |
| 84 | + }; |
| 85 | + |
| 86 | + storage.nested_map_2.get_handle((0, 0)).get_handle("0000").insert(0, m1); |
| 87 | + storage.nested_map_2.get_handle((0, 0)).get_handle("0001").insert(1, m2); |
| 88 | + storage.nested_map_2.get_handle((0, 1)).get_handle("0000").insert(0, m1); |
| 89 | + storage.nested_map_2.get_handle((0, 1)).get_handle("0001").insert(1, m2); |
| 90 | + storage.nested_map_2.get_handle((1, 0)).get_handle("0000").insert(0, m1); |
| 91 | + storage.nested_map_2.get_handle((1, 0)).get_handle("0001").insert(1, m2); |
| 92 | + storage.nested_map_2.get_handle((1, 1)).get_handle("0000").insert(0, m1); |
| 93 | + storage.nested_map_2.get_handle((1, 1)).get_handle("0001").insert(1, m2); |
| 94 | + |
| 95 | + assert(storage.nested_map_2.get_handle((0, 0)).get_handle("0000").get(0).unwrap() == m1); |
| 96 | + assert(storage.nested_map_2.get_handle((0, 0)).get_handle("0001").get(1).unwrap() == m2); |
| 97 | + assert(storage.nested_map_2.get_handle((0, 1)).get_handle("0000").get(0).unwrap() == m1); |
| 98 | + assert(storage.nested_map_2.get_handle((0, 1)).get_handle("0001").get(1).unwrap() == m2); |
| 99 | + assert(storage.nested_map_2.get_handle((1, 0)).get_handle("0000").get(0).unwrap() == m1); |
| 100 | + assert(storage.nested_map_2.get_handle((1, 0)).get_handle("0001").get(1).unwrap() == m2); |
| 101 | + assert(storage.nested_map_2.get_handle((1, 1)).get_handle("0000").get(0).unwrap() == m1); |
| 102 | + assert(storage.nested_map_2.get_handle((1, 1)).get_handle("0001").get(1).unwrap() == m2); |
| 103 | + } |
| 104 | + |
| 105 | + #[storage(read, write)] |
| 106 | + fn nested_map_3_access() { |
| 107 | + let m1 = M { |
| 108 | + u: 0x1111111111111111111111111111111111111111111111111111111111111111, |
| 109 | + v: 1 |
| 110 | + }; |
| 111 | + let m2 = M { |
| 112 | + u: 0x2222222222222222222222222222222222222222222222222222222222222222, |
| 113 | + v: 2 |
| 114 | + }; |
| 115 | + let e1 = E::A(42); |
| 116 | + let e2 = E::B(0x3333333333333333333333333333333333333333333333333333333333333333); |
| 117 | + |
| 118 | + storage.nested_map_3.get_handle(0).get_handle(m1).insert(0, e1); |
| 119 | + storage.nested_map_3.get_handle(0).get_handle(m2).insert(1, e2); |
| 120 | + storage.nested_map_3.get_handle(0).get_handle(m1).insert(0, e1); |
| 121 | + storage.nested_map_3.get_handle(0).get_handle(m2).insert(1, e2); |
| 122 | + storage.nested_map_3.get_handle(1).get_handle(m1).insert(0, e1); |
| 123 | + storage.nested_map_3.get_handle(1).get_handle(m2).insert(1, e2); |
| 124 | + storage.nested_map_3.get_handle(1).get_handle(m1).insert(0, e1); |
| 125 | + storage.nested_map_3.get_handle(1).get_handle(m2).insert(1, e2); |
| 126 | + |
| 127 | + assert(storage.nested_map_3.get_handle(0).get_handle(m1).get(0).unwrap() == e1); |
| 128 | + assert(storage.nested_map_3.get_handle(0).get_handle(m2).get(1).unwrap() == e2); |
| 129 | + assert(storage.nested_map_3.get_handle(0).get_handle(m1).get(0).unwrap() == e1); |
| 130 | + assert(storage.nested_map_3.get_handle(0).get_handle(m2).get(1).unwrap() == e2); |
| 131 | + assert(storage.nested_map_3.get_handle(1).get_handle(m1).get(0).unwrap() == e1); |
| 132 | + assert(storage.nested_map_3.get_handle(1).get_handle(m2).get(1).unwrap() == e2); |
| 133 | + assert(storage.nested_map_3.get_handle(1).get_handle(m1).get(0).unwrap() == e1); |
| 134 | + assert(storage.nested_map_3.get_handle(1).get_handle(m2).get(1).unwrap() == e2); |
| 135 | + } |
| 136 | +} |
0 commit comments