Skip to content
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

Copy ut from https://github.com/neo-project/neo/pull/947 #952

Merged
merged 1 commit into from
Jul 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions neo.UnitTests/UT_DataCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO.Caching;
using Neo.Ledger;
using System.Linq;

namespace Neo.UnitTests
{
[TestClass]
public class UT_DataCache
{
[TestInitialize]
public void TestSetup()
{
TestBlockchain.InitializeMockNeoSystem();
}

[TestMethod]
public void TestCachedFind_Between()
{
var snapshot = TestBlockchain.GetStore().GetSnapshot();
var storages = snapshot.Storages;
var cache = new CloneCache<StorageKey, StorageItem>(storages);

storages.DeleteWhere((k, v) => k.ScriptHash == UInt160.Zero);

storages.Add
(
new StorageKey() { Key = new byte[] { 0x00, 0x01 }, ScriptHash = UInt160.Zero },
new StorageItem() { IsConstant = false, Value = new byte[] { } }
);
storages.Add
(
new StorageKey() { Key = new byte[] { 0x00, 0x03 }, ScriptHash = UInt160.Zero },
new StorageItem() { IsConstant = false, Value = new byte[] { } }
);
cache.Add
(
new StorageKey() { Key = new byte[] { 0x00, 0x02 }, ScriptHash = UInt160.Zero },
new StorageItem() { IsConstant = false, Value = new byte[] { } }
);

CollectionAssert.AreEqual(
cache.Find(new byte[] { 0x00 }).Select(u => u.Key.Key[1]).ToArray(),
new byte[] { 0x01, 0x02, 0x03 }
);

storages.DeleteWhere((k, v) => k.ScriptHash == UInt160.Zero);
}

[TestMethod]
public void TestCachedFind_Last()
{
var snapshot = TestBlockchain.GetStore().GetSnapshot();
var storages = snapshot.Storages;
var cache = new CloneCache<StorageKey, StorageItem>(storages);

storages.DeleteWhere((k, v) => k.ScriptHash == UInt160.Zero);

storages.Add
(
new StorageKey() { Key = new byte[] { 0x00, 0x01 }, ScriptHash = UInt160.Zero },
new StorageItem() { IsConstant = false, Value = new byte[] { } }
);
cache.Add
(
new StorageKey() { Key = new byte[] { 0x00, 0x02 }, ScriptHash = UInt160.Zero },
new StorageItem() { IsConstant = false, Value = new byte[] { } }
);

CollectionAssert.AreEqual(
cache.Find(new byte[] { 0x00 }).Select(u => u.Key.Key[1]).ToArray(),
new byte[] { 0x01, 0x02 }
);

storages.DeleteWhere((k, v) => k.ScriptHash == UInt160.Zero);
}

[TestMethod]
public void TestCachedFind_Empty()
{
var snapshot = TestBlockchain.GetStore().GetSnapshot();
var storages = snapshot.Storages;
var cache = new CloneCache<StorageKey, StorageItem>(storages);

storages.DeleteWhere((k, v) => k.ScriptHash == UInt160.Zero);

cache.Add
(
new StorageKey() { Key = new byte[] { 0x00, 0x02 }, ScriptHash = UInt160.Zero },
new StorageItem() { IsConstant = false, Value = new byte[] { } }
);

CollectionAssert.AreEqual(
cache.Find(new byte[] { 0x00 }).Select(u => u.Key.Key[1]).ToArray(),
new byte[] { 0x02 }
);

storages.DeleteWhere((k, v) => k.ScriptHash == UInt160.Zero);
}
}
}