Skip to content

Commit 6c29bde

Browse files
Jim8yshargon
andauthored
[Neo Core Storage] Implicit methods and tests (#3403)
* implicit methods and tests * udpate --------- Co-authored-by: Shargon <shargon@gmail.com>
1 parent bf50823 commit 6c29bde

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/Neo/SmartContract/StorageItem.cs

+10
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,15 @@ public static implicit operator BigInteger(StorageItem item)
193193
item.cache ??= new BigInteger(item.value.Span);
194194
return (BigInteger)item.cache;
195195
}
196+
197+
public static implicit operator StorageItem(BigInteger value)
198+
{
199+
return new StorageItem(value);
200+
}
201+
202+
public static implicit operator StorageItem(byte[] value)
203+
{
204+
return new StorageItem(value);
205+
}
196206
}
197207
}

src/Neo/SmartContract/StorageKey.cs

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Neo.Cryptography;
1313
using System;
1414
using System.Buffers.Binary;
15+
using System.Runtime.CompilerServices;
1516

1617
namespace Neo.SmartContract
1718
{
@@ -79,5 +80,14 @@ public byte[] ToArray()
7980
}
8081
return cache;
8182
}
83+
84+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
85+
public static implicit operator StorageKey(byte[] value) => new StorageKey(value);
86+
87+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
88+
public static implicit operator StorageKey(ReadOnlyMemory<byte> value) => new StorageKey(value.Span.ToArray());
89+
90+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
91+
public static implicit operator StorageKey(ReadOnlySpan<byte> value) => new StorageKey(value.ToArray());
8292
}
8393
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)