Skip to content

Commit

Permalink
Add Address(ImmutableArray<byte>) constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
kanghyojun committed Aug 16, 2019
1 parent 52e9107 commit 353c99c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Expand Up @@ -29,6 +29,8 @@ To be released.
- Removed `IRandom.NextDouble()` method, because [floating-point arithmetics,
which is underspecified, likely introduce
indeterminism][floating-point determinism]. [[#410], [#419]]
- `Address(byte[])` became to throw `ArgumentNullException`
instead of `NullReferenceException`. [[#443]]

### Added interfaces

Expand All @@ -52,6 +54,7 @@ To be released.
- Added `PreloadState`, `ActionExecutionState`, `StateReferenceDownloadState`,
and `BlockStateDownloadState` classes to cover all phases in the entire
preloading process. [[#397], [#400]]
- Added `Address(ImmutableArray<byte>)` constructor. [[#442], [#443]]

### Behavioral changes

Expand Down Expand Up @@ -146,6 +149,8 @@ To be released.
[#426]: https://github.com/planetarium/libplanet/pull/426
[#434]: https://github.com/planetarium/libplanet/pull/434
[#438]: https://github.com/planetarium/libplanet/pull/438
[#442]: https://github.com/planetarium/libplanet/issues/442
[#443]: https://github.com/planetarium/libplanet/pull/443
[LiteDB #1268]: https://github.com/mbdavid/LiteDB/issues/1268
[floating-point determinism]: https://wp.me/p1fTCO-kT

Expand Down
19 changes: 18 additions & 1 deletion Libplanet.Tests/AddressTest.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
Expand All @@ -12,11 +13,27 @@ public class AddressTest
[Fact]
public void ConstructorDoesNotTakeNullValue()
{
Assert.Throws<NullReferenceException>(
Assert.Throws<ArgumentNullException>(
() => new Address((byte[])null)
);
}

[Fact]
public void ConstructWithImmutableArray()
{
byte[] addr = new byte[20]
{
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xab,
0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab,
0xcd, 0xef,
};

Assert.Equal(
new Address("0123456789ABcdefABcdEfABcdEFabcDEFabCDEF"),
new Address(addr.ToImmutableArray())
);
}

[Fact]
public void DefaultConstructor()
{
Expand Down
38 changes: 24 additions & 14 deletions Libplanet/Address.cs
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.Serialization;
using System.Text;
using Libplanet.Crypto;
Expand Down Expand Up @@ -50,14 +48,36 @@ public struct Address : ISerializable, IComparable<Address>, IComparable

private ImmutableArray<byte> _byteArray;

/// <summary>
/// Creates an <see cref="Address"/> instance from the given immutable <see
/// cref="byte"/> array (i.e., <paramref name="address"/>).
/// </summary>
/// <param name="address">An immutable array of 20 <see cref="byte"/>s which
/// represents an <see cref="Address"/>.</param>
/// <exception cref="ArgumentException">Thrown when the given <paramref
/// name="address"/> array did not lengthen 20 bytes.</exception>
/// <remarks>A valid <see cref="byte"/> array which represents an
/// <see cref="Address"/> can be gotten using <see cref="ToByteArray()"
/// /> method.</remarks>
/// <seealso cref="ByteArray"/>
public Address(ImmutableArray<byte> address)
{
if (address.Length != Size)
{
throw new ArgumentException("address must be 20 bytes", nameof(address));
}

_byteArray = address;
}

/// <summary>
/// Creates an <see cref="Address"/> instance from the given <see
/// cref="byte"/> array (i.e., <paramref name="address"/>).
/// </summary>
/// <param name="address">An array of 20 <see cref="byte"/>s which
/// represents an <see cref="Address"/>. This must not be <c>null</c>.
/// </param>
/// <exception cref="NullReferenceException">Thrown when <c>null</c> was
/// <exception cref="ArgumentNullException">Thrown when <c>null</c> was
/// passed to <paramref name="address"/>.</exception>
/// <exception cref="ArgumentException">Thrown when the given <paramref
/// name="address"/> array did not lengthen 20 bytes.</exception>
Expand All @@ -66,18 +86,8 @@ public struct Address : ISerializable, IComparable<Address>, IComparable
/// /> method.</remarks>
/// <seealso cref="ToByteArray()"/>
public Address(byte[] address)
: this(address?.ToImmutableArray() ?? throw new ArgumentNullException(nameof(address)))
{
if (address == null)
{
throw new NullReferenceException("address must not be null");
}

if (address.Length != Size)
{
throw new ArgumentException("address must be 20 bytes");
}

_byteArray = address.ToImmutableArray();
}

public Address(
Expand Down

0 comments on commit 353c99c

Please sign in to comment.