|
| 1 | +// Copyright (C) 2015-2024 The Neo Project. |
| 2 | +// |
| 3 | +// UT_Storage.cs file belongs to the neo project and is free |
| 4 | +// software distributed under the MIT software license, see the |
| 5 | +// accompanying file LICENSE in the main directory of the |
| 6 | +// repository or http://www.opensource.org/licenses/mit-license.php |
| 7 | +// for more details. |
| 8 | +// |
| 9 | +// Redistribution and use in source and binary forms with or without |
| 10 | +// modifications are permitted. |
| 11 | + |
| 12 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 13 | +using Neo.SmartContract; |
| 14 | +using Neo.VM; |
| 15 | +using Neo.VM.Types; |
| 16 | +using System; |
| 17 | +using System.Linq; |
| 18 | +using System.Numerics; |
| 19 | + |
| 20 | +namespace Neo.UnitTests.SmartContract |
| 21 | +{ |
| 22 | + [TestClass] |
| 23 | + public class UT_Storage |
| 24 | + { |
| 25 | + [TestMethod] |
| 26 | + public void TestStorageKey() |
| 27 | + { |
| 28 | + // Test data |
| 29 | + byte[] keyData = [0x00, 0x00, 0x00, 0x00, 0x12]; |
| 30 | + var keyMemory = new ReadOnlyMemory<byte>(keyData); |
| 31 | + |
| 32 | + // Test implicit conversion from byte[] to StorageKey |
| 33 | + StorageKey storageKeyFromArray = keyData; |
| 34 | + Assert.AreEqual(0, storageKeyFromArray.Id); |
| 35 | + Assert.IsTrue(keyMemory.Span.ToArray().Skip(sizeof(int)).SequenceEqual(storageKeyFromArray.Key.Span.ToArray())); |
| 36 | + |
| 37 | + // Test implicit conversion from ReadOnlyMemory<byte> to StorageKey |
| 38 | + StorageKey storageKeyFromMemory = keyMemory; |
| 39 | + Assert.AreEqual(0, storageKeyFromMemory.Id); |
| 40 | + Assert.IsTrue(keyMemory.Span.ToArray().Skip(sizeof(int)).SequenceEqual(storageKeyFromMemory.Key.Span.ToArray())); |
| 41 | + |
| 42 | + // Test CreateSearchPrefix method |
| 43 | + byte[] prefix = { 0xAA }; |
| 44 | + var searchPrefix = StorageKey.CreateSearchPrefix(0, prefix); |
| 45 | + var expectedPrefix = BitConverter.GetBytes(0).Concat(prefix).ToArray(); |
| 46 | + Assert.IsTrue(expectedPrefix.SequenceEqual(searchPrefix)); |
| 47 | + |
| 48 | + // Test Equals method |
| 49 | + var storageKey1 = new StorageKey { Id = 0, Key = keyMemory }; |
| 50 | + var storageKey2 = new StorageKey { Id = 0, Key = keyMemory }; |
| 51 | + var storageKeyDifferentId = new StorageKey { Id = 0 + 1, Key = keyMemory }; |
| 52 | + var storageKeyDifferentKey = new StorageKey { Id = 0, Key = new ReadOnlyMemory<byte>([0x04]) }; |
| 53 | + Assert.AreEqual(storageKey1, storageKey2); |
| 54 | + Assert.AreNotEqual(storageKey1, storageKeyDifferentId); |
| 55 | + Assert.AreNotEqual(storageKey1, storageKeyDifferentKey); |
| 56 | + } |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void TestStorageItem() |
| 60 | + { |
| 61 | + // Test data |
| 62 | + byte[] keyData = [0x00, 0x00, 0x00, 0x00, 0x12]; |
| 63 | + BigInteger bigInteger = new BigInteger(1234567890); |
| 64 | + |
| 65 | + // Test implicit conversion from byte[] to StorageItem |
| 66 | + StorageItem storageItemFromArray = keyData; |
| 67 | + Assert.IsTrue(keyData.SequenceEqual(storageItemFromArray.Value.Span.ToArray())); |
| 68 | + |
| 69 | + // Test implicit conversion from BigInteger to StorageItem |
| 70 | + StorageItem storageItemFromBigInteger = bigInteger; |
| 71 | + Assert.AreEqual(bigInteger, (BigInteger)storageItemFromBigInteger); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments