public void AppendManyWithOffset() { var rand = new Random(42); var pack1 = new CoinPack(new byte[4096]); pack1.SetOutpointSigs(GetSigs(rand)); var coin1 = GetCoin(rand); var coin2 = GetCoin(rand); var coin3 = GetCoin(rand); pack1.Append(coin1); pack1.Append(coin2); pack1.Append(coin3); var pack2 = new CoinPack(new byte[4096]); pack2.Append(pack1, 1, 2); Assert.Equal(2, pack2.CoinCount); pack2.TryGet(ref coin1.Outpoint, out Coin g1); Assert.True(g1.IsEmpty); pack2.TryGet(ref coin2.Outpoint, out Coin g2); Assert.True(coin2.Span.SequenceEqual(g2.Span)); pack2.TryGet(ref coin3.Outpoint, out Coin g3); Assert.True(coin3.Span.SequenceEqual(g3.Span)); }
private Span <byte> ReadSpan(int layerIndex, uint sectorIndex) { if (layerIndex >= _layers.Length) { if (!_bottomLayer.TryGet(sectorIndex, out var span)) { // In case where key sectorIndex doesn't exist, // we initialize a empty array as value for this key. // We can see this as lazy sector allocation (only // for the bottom layer). Span <byte> mem = stackalloc byte[InitialBottomLayerSectorSize]; var pack = new CoinPack(mem) { SectorIndex = sectorIndex, LayerIndex = (byte)layerIndex }; _bottomLayer.Set(sectorIndex, mem); _bottomLayer.TryGet(sectorIndex, out span); } return(span); } else { return(_layers[layerIndex].Slice((int)(sectorIndex * _sectorSizeInBytes[layerIndex]), _sectorSizeInBytes[layerIndex]).Span); } }
public void GetOnEmptyCoinPack() { var rand = new Random(42); var pack = new CoinPack(new byte[4096]); var coin = GetCoin(rand); Assert.False(pack.TryGet(ref coin.Outpoint, out Coin g1)); Assert.True(g1.IsEmpty); }
public void CoinCountAfterAppend() { var pack = new CoinPack(new byte[4096]); var coin = new Coin(new byte[128]); pack.Append(coin); Assert.Equal(1, pack.CoinCount); pack.Append(coin); Assert.Equal(2, pack.CoinCount); }
/// <summary> /// FORMAT = [C:x,y:lt:val#] /// </summary> /// <param name="command">Command.</param> private void CoinSpawnExec(string command) { try { var coins = command.Substring(2).Split(':'); var location = coins [0].Split(','); var coinPack = new CoinPack(int.Parse(location [0]), int.Parse(location [1]), int.Parse(coins [1]), int.Parse(coins [2])); coinpacks.Add(coinPack); // Generate Coin Packs GenerateGameObjects.GetInstance().GenerateCoinPacks(coinPack); } catch (Exception ex) { Debug.LogException(ex); } }
public void GetSetOutpointSigs() { var rand = new Random(42); var pack = new CoinPack(new byte[4096]); var sigs = GetSigs(rand); pack.SetOutpointSigs(sigs); Assert.Equal(sigs.Length, pack.OutpointSigCount); var shallow = pack.OutpointSigs; Assert.True(shallow.SequenceEqual(sigs)); }
public void CountCoinsAbove() { var rand = new Random(42); var pack = new CoinPack(new byte[4096]); pack.SetOutpointSigs(GetSigs(rand)); for (var i = 0; i < 10; i++) { pack.Append(GetCoin(rand)); } Assert.Equal(1, pack.CountCoinsAbove(10)); Assert.Equal(3, pack.CountCoinsAbove(500)); Assert.Equal(10, pack.CountCoinsAbove(10000)); }
public void GetSetSectorProperties() { var pack = new CoinPack(new byte[4096]); Assert.Equal(0u, pack.SectorIndex); Assert.Equal(0, pack.LayerIndex); Assert.Equal(0, pack.WriteColor); Assert.Equal(0, pack.WriteCount); pack.SectorIndex = 42; pack.LayerIndex = 7; pack.WriteColor = 1; pack.WriteCount = 3; Assert.Equal(42u, pack.SectorIndex); Assert.Equal(7, pack.LayerIndex); Assert.Equal(1, pack.WriteColor); Assert.Equal(3, pack.WriteCount); }
public CoinPack Read(int layerIndex, uint sectorIndex) { var span = ReadSpan(layerIndex, sectorIndex); var pack = new CoinPack(span); // storage pack header not initialized, we initialize it: if (pack.SectorIndex == 0 && pack.LayerIndex == 0) { pack.SectorIndex = sectorIndex; pack.LayerIndex = (byte)layerIndex; } if (pack.SectorIndex != sectorIndex) { throw new InvalidOperationException("SectorIndex mismatch."); } return(pack); }
public void GetSpan() { var rand = new Random(42); var pack = new CoinPack(new byte[4096]); pack.SetOutpointSigs(GetSigs(rand)); var coin1 = GetCoin(rand); var coin2 = GetCoin(rand); pack.Append(coin1); pack.Append(coin2); var shallow = new CoinPack(pack.Span); shallow.TryGet(ref coin2.Outpoint, out Coin g2); Assert.True(coin2.Span.SequenceEqual(g2.Span)); }
private void WriteSector(uint sectorIndex, Span <byte> s0, Span <byte> s1, Span <byte> s2, Span <byte> s3) { var p0 = new CoinPack(s0) { LayerIndex = 0, SectorIndex = sectorIndex, Version = CoinPack.CurrentVersion, SizeInBytes = _fixture.SectorsSize[0] }; var p1 = new CoinPack(s1) { LayerIndex = 1, SectorIndex = sectorIndex, Version = CoinPack.CurrentVersion, SizeInBytes = _fixture.SectorsSize[1] }; var p2 = new CoinPack(s2) { LayerIndex = 2, SectorIndex = sectorIndex, Version = CoinPack.CurrentVersion, SizeInBytes = _fixture.SectorsSize[2] }; var collection = new ShortCoinPackCollection(p0); collection.Add(p1); collection.Add(p2); if (!s3.IsEmpty) { var p3 = new CoinPack(s3) { LayerIndex = 3, SectorIndex = sectorIndex, Version = CoinPack.CurrentVersion, SizeInBytes = s3.Length }; collection.Add(p3); } try { _fixture.CreateStore(); _fixture.Store.Write(collection); } finally { _fixture.CloseStore(); } }
public void AppendCoin() { var rand = new Random(42); var pack = new CoinPack(new byte[4096]); pack.SetOutpointSigs(GetSigs(rand)); var coin1 = GetCoin(rand); var coin2 = GetCoin(rand); var coin3 = GetCoin(rand); pack.Append(coin1); Assert.Equal(1, pack.CoinCount); pack.Append(coin2); Assert.Equal(2, pack.CoinCount); pack.Append(coin3); Assert.Equal(3, pack.CoinCount); pack.TryGet(ref coin1.Outpoint, out Coin g1); Assert.True(coin1.Span.SequenceEqual(g1.Span)); pack.TryGet(ref coin2.Outpoint, out Coin g2); Assert.True(coin2.Span.SequenceEqual(g2.Span)); pack.TryGet(ref coin3.Outpoint, out Coin g3); Assert.True(coin3.Span.SequenceEqual(g3.Span)); // should not be found var coin4 = GetCoin(rand); pack.TryGet(ref coin4.Outpoint, out Coin g4); Assert.True(g4.IsEmpty); }
public void Empty() { var pack = new CoinPack(new byte[4096]); Assert.Equal(0, pack.CoinCount); }
public void GenerateCoinPacks(CoinPack coinPack) { var obj = UpdateGameObject(coinPack.ToString(), CoinPack, new Vector3(coinPack.x * xFactor, coinPack.y * yFactor, zPos), originalRot); Destroy(obj, ((float)coinPack.lifetime) / 1000); }