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

Refactoring Pointer #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Refactoring Pointer #10

wants to merge 2 commits into from

Conversation

rizamarhaban
Copy link
Collaborator

Refactoring Pointer Struct

Features

Create an empty pointer can now be like this:

Pointer pointer = Pointer.Empty;

Pointer equality comparer:

Pointer pointerA = new Pointer(1, 2, 45, true);
Pointer pointerB = new Pointer(1, 2, 45, true);

pointerA == pointerB;   // true

Pointer Superposition

Pointer pointerA= new Pointer(1, 2, 45, true);
Pointer pointerB = new Pointer(2, 3, 45, true);

var mulPointer = pointerA * pointerB;
var divpointer = mulPointer / pointerB;

divpointer == pointerA;   // true

UnitTests Passed

image

Adding UnitTest for serializing and deserializing Pointer

[Test]
public void Created_Pointer_Can_Be_Serialized_As_Json_And_Deserialized_Back()
{
    Pointer pointerA = new Pointer(1, 2, 45, true);

    // this option is just to make the json formatting pretty
    // and able to convert Pointer struct using custom converter
    var jsonOption = new JsonSerializerOptions { WriteIndented = true };
    jsonOption.Converters.Add(new PointerJsonConverter());

    // first, serialize Pointer as json string
    var json = JsonSerializer.Serialize(pointerA, jsonOption);
    TestContext.WriteLine("Output pointerA as Json");
    TestContext.WriteLine(json);

    // second, deserialize json string back to pointer
    var pointerB = JsonSerializer.Deserialize<Pointer>(json, jsonOption);

    // the result should be equal Pointer
    Assert.AreEqual(pointerA, pointerB);
}

The output:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant