-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Neo Core Storage] Implicit methods and tests #3403
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// UT_Storage.cs file belongs to the neo project and is free | ||
// software distributed under the MIT software license, see the | ||
// accompanying file LICENSE in the main directory of the | ||
// repository or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.SmartContract; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.Linq; | ||
using System.Numerics; | ||
|
||
namespace Neo.UnitTests.SmartContract | ||
{ | ||
[TestClass] | ||
public class UT_Storage | ||
{ | ||
[TestMethod] | ||
public void TestStorageKey() | ||
{ | ||
// Test data | ||
byte[] keyData = [0x00, 0x00, 0x00, 0x00, 0x12]; | ||
var keyMemory = new ReadOnlyMemory<byte>(keyData); | ||
|
||
// Test implicit conversion from byte[] to StorageKey | ||
StorageKey storageKeyFromArray = keyData; | ||
Assert.AreEqual(0, storageKeyFromArray.Id); | ||
Assert.IsTrue(keyMemory.Span.ToArray().Skip(sizeof(int)).SequenceEqual(storageKeyFromArray.Key.Span.ToArray())); | ||
|
||
// Test implicit conversion from ReadOnlyMemory<byte> to StorageKey | ||
StorageKey storageKeyFromMemory = keyMemory; | ||
Assert.AreEqual(0, storageKeyFromMemory.Id); | ||
Assert.IsTrue(keyMemory.Span.ToArray().Skip(sizeof(int)).SequenceEqual(storageKeyFromMemory.Key.Span.ToArray())); | ||
|
||
// Test CreateSearchPrefix method | ||
byte[] prefix = { 0xAA }; | ||
var searchPrefix = StorageKey.CreateSearchPrefix(0, prefix); | ||
var expectedPrefix = BitConverter.GetBytes(0).Concat(prefix).ToArray(); | ||
Assert.IsTrue(expectedPrefix.SequenceEqual(searchPrefix)); | ||
|
||
// Test Equals method | ||
var storageKey1 = new StorageKey { Id = 0, Key = keyMemory }; | ||
var storageKey2 = new StorageKey { Id = 0, Key = keyMemory }; | ||
var storageKeyDifferentId = new StorageKey { Id = 0 + 1, Key = keyMemory }; | ||
var storageKeyDifferentKey = new StorageKey { Id = 0, Key = new ReadOnlyMemory<byte>([0x04]) }; | ||
Assert.AreEqual(storageKey1, storageKey2); | ||
Assert.AreNotEqual(storageKey1, storageKeyDifferentId); | ||
Assert.AreNotEqual(storageKey1, storageKeyDifferentKey); | ||
} | ||
|
||
[TestMethod] | ||
public void TestStorageItem() | ||
{ | ||
// Test data | ||
byte[] keyData = [0x00, 0x00, 0x00, 0x00, 0x12]; | ||
BigInteger bigInteger = new BigInteger(1234567890); | ||
|
||
// Test implicit conversion from byte[] to StorageItem | ||
StorageItem storageItemFromArray = keyData; | ||
Assert.IsTrue(keyData.SequenceEqual(storageItemFromArray.Value.Span.ToArray())); | ||
|
||
// Test implicit conversion from BigInteger to StorageItem | ||
StorageItem storageItemFromBigInteger = bigInteger; | ||
Assert.AreEqual(bigInteger, (BigInteger)storageItemFromBigInteger); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just wondering, do you have a use-case for these conversion operators in the code? Usually when we introduce such helpers, there's a need to use them in the code and we replace some real code with these helpers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't matter how it done. As long as the do the same thing. We have direct access to the
class
. So, no need for extension methods. Plus this doesn't convert toBigInteger
its allows us tocompare
the two.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it matter ?