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

Fix UT for PR#2215 #2220

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
70fe372
Update UT_DataCache.cs
chenzhitong Jan 12, 2021
f137320
Remove SerializableWrapper, Update UT_CloneCache
chenzhitong Jan 12, 2021
b28e797
update
chenzhitong Jan 12, 2021
50d4c65
NativeContract.Ledger.GetBlock() / NativeContract.Ledger.GetTransacti…
chenzhitong Jan 12, 2021
c0c0809
Remove GetTransactionHeight and GetBlockchainHeight
chenzhitong Jan 12, 2021
a885b6f
Some fixes
shargon Jan 12, 2021
eb3503e
More fixes
shargon Jan 12, 2021
5bbdb15
Some fixes
shargon Jan 12, 2021
53916fd
More fixes
shargon Jan 12, 2021
5a095ef
Some fixes
shargon Jan 12, 2021
14f3882
Remove internal
shargon Jan 12, 2021
decdd4f
Fix UT_SmartContractHelper
shargon Jan 12, 2021
60a51c4
Fix UT_PolicyContract
shargon Jan 12, 2021
ced82bc
Fix UT_TransactionState
shargon Jan 12, 2021
105713a
Fix data cache ut
shargon Jan 12, 2021
0e4471e
More fixes
shargon Jan 12, 2021
963ed82
format
erikzhang Jan 14, 2021
ee0caca
Revert changes on StorageItem
shargon Jan 14, 2021
6475d5c
Remove unnecessary change
erikzhang Jan 14, 2021
eff2e9d
Fix UT_TrimmedBlock
shargon Jan 14, 2021
14d3161
Merge remote-tracking branch 'chenzhitong/fix-ut/ledger-contract' int…
shargon Jan 14, 2021
aa4b22f
Merge branch 'native/ledger-contract' into fix-ut/ledger-contract
erikzhang Jan 14, 2021
c413723
Update NonfungibleToken.cs
chenzhitong Jan 14, 2021
26dd1e9
Fix one UT
shargon Jan 14, 2021
0b17410
Fix other UT
shargon Jan 14, 2021
ff985cc
Merge remote-tracking branch 'chenzhitong/fix-ut/ledger-contract' int…
shargon Jan 14, 2021
177995e
Fix last ut
shargon Jan 14, 2021
4fc7ac2
Fix Equals
erikzhang Jan 14, 2021
73cdfa0
Recover UT
shargon Jan 14, 2021
9b70655
Merge remote-tracking branch 'chenzhitong/fix-ut/ledger-contract' int…
shargon Jan 14, 2021
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
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/HashIndexState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Neo.SmartContract.Native
{
class HashIndexState : IInteroperable
internal class HashIndexState : IInteroperable
{
public UInt256 Hash;
public uint Index;
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/LedgerContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private Transaction GetTransactionFromBlock(DataCache snapshot, byte[] blockInde
return GetTransaction(snapshot, block.Hashes[txIndex + 1]);
}

private static TrimmedBlock Trim(Block block)
internal static TrimmedBlock Trim(Block block)
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
return new TrimmedBlock
{
Expand Down
16 changes: 15 additions & 1 deletion src/neo/SmartContract/StorageItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Neo.SmartContract
{
public class StorageItem : ISerializable
public class StorageItem : ISerializable, IEquatable<StorageItem>
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
private byte[] value;
private object cache;
Expand Down Expand Up @@ -112,6 +113,19 @@ public void Set(BigInteger integer)
value = null;
}

public bool Equals(StorageItem other)
{
if (other == null) return false;

return IsConstant == other.IsConstant &&
Value.SequenceEqual(other.Value);
}

public override int GetHashCode()
{
return HashCode.Combine(IsConstant, Value.Length);
}

public static implicit operator BigInteger(StorageItem item)
{
item.cache ??= new BigInteger(item.value);
Expand Down
11 changes: 6 additions & 5 deletions tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Neo.UnitTests.IO.Caching
{
[TestClass]
public class UT_CloneCache
{
ClonedCache clonedCache;
MyDataCache<MyKey, MyValue> myDataCache;
MyDataCache myDataCache;

[TestInitialize]
public void Init()
{
myDataCache = new MyDataCache<MyKey, MyValue>();
myDataCache = new MyDataCache();
clonedCache = new ClonedCache(myDataCache);
}

Expand Down Expand Up @@ -112,9 +113,9 @@ public void TestUpdateInternal()
myDataCache.Add(new MyKey("key2"), new MyValue("value2"));
myDataCache.InnerDict.Add(new MyKey("key3"), new MyValue("value3"));

clonedCache.GetAndChange(new MyKey("key1")).Value = "value_new_1";
clonedCache.GetAndChange(new MyKey("key2")).Value = "value_new_2";
clonedCache.GetAndChange(new MyKey("key3")).Value = "value_new_3";
clonedCache.GetAndChange(new MyKey("key1")).Value = Encoding.Default.GetBytes("value_new_1");
clonedCache.GetAndChange(new MyKey("key2")).Value = Encoding.Default.GetBytes("value_new_2");
clonedCache.GetAndChange(new MyKey("key3")).Value = Encoding.Default.GetBytes("value_new_3");

clonedCache.Commit();

Expand Down
112 changes: 48 additions & 64 deletions tests/neo.UnitTests/IO/Caching/UT_DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,70 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Persistence;
using Neo.SmartContract;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Neo.UnitTests.IO.Caching
{
class MyKey : ISerializable, IEquatable<MyKey>, IComparable<MyKey>
class MyKey : StorageKey, IEquatable<MyKey>
{
public string Key;

public int Size => Key.Length;

public MyKey() { }
public MyKey(UInt256 hash)
{
Key = hash.ToArray();
}

public MyKey(string val)
{
Key = val;
Key = Encoding.Default.GetBytes(val);
}

public void Deserialize(BinaryReader reader)
{
Key = reader.ReadString();
Key = Encoding.Default.GetBytes(reader.ReadString());
}

public void Serialize(BinaryWriter writer)
{
writer.Write(Key);
}

public bool Equals(MyKey other)
{
return Key.Equals(other.Key);
return Key.SequenceEqual(other.Key);
}

public override bool Equals(object obj)
{
if (!(obj is MyKey key)) return false;
return Equals(key);
if (obj is not MyKey other) return false;
return Key.SequenceEqual(other.Key);
}

public override int GetHashCode()
{
return Key.GetHashCode();
}

public int CompareTo(MyKey obj)
{
return Key.CompareTo(obj.Key);
return Key.Length;
}
}

public class MyValue : ISerializable
public class MyValue : StorageItem, ISerializable, IEquatable<MyValue>
{
public string Value;

public int Size => Value.Length;

public MyValue() { }

public MyValue(string val)
{
Value = val;
}

public void Deserialize(BinaryReader reader)
public MyValue(UInt256 hash)
{
Value = reader.ReadString();
Value = hash.ToArray();
}

public void Serialize(BinaryWriter writer)
public MyValue(string val)
{
writer.Write(Value);
Value = Encoding.Default.GetBytes(val);
}

public MyValue Clone()
public MyValue(byte[] val)
{
return new MyValue(Value);
Value = val;
}

public void FromReplica(MyValue replica)
Expand All @@ -89,83 +75,81 @@ public void FromReplica(MyValue replica)

public bool Equals(MyValue other)
{
return (Value == null && other.Value == null) || Value.Equals(other.Value);
return (Value == null && other.Value == null) || Value.SequenceEqual(other.Value);
}

public override bool Equals(object obj)
{
if (!(obj is MyValue key)) return false;
return Equals(key);
if (obj is not StorageItem other) return false;
return (Value == null && other.Value == null) || Value.SequenceEqual(other.Value);
}

public override int GetHashCode()
{
return Value.GetHashCode();
return Value.Length;
}
}

class MyDataCache<TKey, TValue> : DataCache
where TKey : IEquatable<TKey>, ISerializable, new()
where TValue : class, ISerializable, new()
class MyDataCache : DataCache
{
public Dictionary<TKey, TValue> InnerDict = new Dictionary<TKey, TValue>();
public Dictionary<StorageKey, StorageItem> InnerDict = new Dictionary<StorageKey, StorageItem>();

protected override void DeleteInternal(TKey key)
protected override void DeleteInternal(StorageKey key)
{
InnerDict.Remove(key);
}

protected override void AddInternal(TKey key, TValue value)
protected override void AddInternal(StorageKey key, StorageItem value)
{
InnerDict.Add(key, value);
}

protected override IEnumerable<(TKey, TValue)> SeekInternal(byte[] keyOrPrefix, SeekDirection direction = SeekDirection.Forward)
protected override IEnumerable<(StorageKey, StorageItem)> SeekInternal(byte[] keyOrPrefix, SeekDirection direction = SeekDirection.Forward)
{
if (direction == SeekDirection.Forward)
return InnerDict.OrderBy(kvp => kvp.Key).Where(kvp => ByteArrayComparer.Default.Compare(kvp.Key.ToArray(), keyOrPrefix) >= 0).Select(p => (p.Key, p.Value));
else
return InnerDict.OrderByDescending(kvp => kvp.Key).Where(kvp => ByteArrayComparer.Reverse.Compare(kvp.Key.ToArray(), keyOrPrefix) >= 0).Select(p => (p.Key, p.Value));
}

protected override TValue GetInternal(TKey key)
protected override StorageItem GetInternal(StorageKey key)
{
if (InnerDict.TryGetValue(key, out TValue value))
if (InnerDict.TryGetValue(key, out var value))
{
return value.Clone();
return new MyValue(value.Value);
}
throw new KeyNotFoundException();
}

protected override TValue TryGetInternal(TKey key)
protected override StorageItem TryGetInternal(StorageKey key)
{
if (InnerDict.TryGetValue(key, out TValue value))
if (InnerDict.TryGetValue(key, out var value))
{
return value.Clone();
return new MyValue(value.Value);
}
return null;
}

protected override bool ContainsInternal(TKey key)
protected override bool ContainsInternal(StorageKey key)
{
return InnerDict.ContainsKey(key);
}

protected override void UpdateInternal(TKey key, TValue value)
protected override void UpdateInternal(StorageKey key, StorageItem value)
{
InnerDict[key] = value;
InnerDict[key] = new MyValue(value.Value);
}
}

[TestClass]
public class UT_DataCache
{
MyDataCache<MyKey, MyValue> myDataCache;
MyDataCache myDataCache;

[TestInitialize]
public void Initialize()
{
myDataCache = new MyDataCache<MyKey, MyValue>();
myDataCache = new MyDataCache();
}

[TestMethod]
Expand Down Expand Up @@ -291,10 +275,10 @@ public void TestSeek()
items[0].Value.Should().Be(new MyValue("value3"));
items[1].Key.Should().Be(new MyKey("key2"));
items[1].Value.Should().Be(new MyValue("value2"));
items.Count().Should().Be(3);
items.Length.Should().Be(3);

items = myDataCache.Seek(new MyKey("key5").ToArray(), SeekDirection.Forward).ToArray();
items.Count().Should().Be(0);
items.Length.Should().Be(0);
}

[TestMethod]
Expand All @@ -311,11 +295,11 @@ public void TestFindRange()
items[0].Value.Should().Be(new MyValue("value3"));
items[1].Key.Should().Be(new MyKey("key4"));
items[1].Value.Should().Be(new MyValue("value4"));
items.Count().Should().Be(2);
items.Length.Should().Be(2);

// case 2 Need to sort the cache of myDataCache

myDataCache = new MyDataCache<MyKey, MyValue>();
myDataCache = new MyDataCache();
myDataCache.Add(new MyKey("key1"), new MyValue("value1"));
myDataCache.Add(new MyKey("key2"), new MyValue("value2"));

Expand All @@ -327,11 +311,11 @@ public void TestFindRange()
items[0].Value.Should().Be(new MyValue("value3"));
items[1].Key.Should().Be(new MyKey("key4"));
items[1].Value.Should().Be(new MyValue("value4"));
items.Count().Should().Be(2);
items.Length.Should().Be(2);

// case 3 FindRange by Backward

myDataCache = new MyDataCache<MyKey, MyValue>();
myDataCache = new MyDataCache();
myDataCache.Add(new MyKey("key1"), new MyValue("value1"));
myDataCache.Add(new MyKey("key2"), new MyValue("value2"));

Expand All @@ -344,7 +328,7 @@ public void TestFindRange()
items[0].Value.Should().Be(new MyValue("value5"));
items[1].Key.Should().Be(new MyKey("key4"));
items[1].Value.Should().Be(new MyValue("value4"));
items.Count().Should().Be(2);
items.Length.Should().Be(2);
}

[TestMethod]
Expand Down Expand Up @@ -413,7 +397,7 @@ public void TestTryGet()
[TestMethod]
public void TestFindInvalid()
{
var myDataCache = new MyDataCache<MyKey, MyValue>();
var myDataCache = new MyDataCache();
myDataCache.Add(new MyKey("key1"), new MyValue("value1"));

myDataCache.InnerDict.Add(new MyKey("key2"), new MyValue("value2"));
Expand Down
Loading