Skip to content

Commit

Permalink
new and safe comparisons (aux for tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaDillenburg committed Aug 20, 2020
1 parent 9274db4 commit acdb419
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Assembler/utils/comparisons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils

func SafeIsEqualStrPointer(a *string, b *string) bool {
if a == nil && b == nil {
return true
}
if a == nil && b != nil {
return false
}
if a != nil && b == nil {
return false
}

return *a == *b
}
24 changes: 24 additions & 0 deletions Assembler/utils/comparisons_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import "testing"

func TestSafeStrPointerComparation(t *testing.T) {
var tests = []struct {
param1 *string
param2 *string
expected bool
}{
{nil, nil, true},
{NewString("Hello"), nil, false},
{nil, NewString("Hello"), false},
{NewString("Hello"), NewString("Hello"), true},
{NewString("Hello"), NewString("Hella"), false},
}

for i, test := range tests {
got := SafeIsEqualStrPointer(test.param1, test.param2)
if got != test.expected {
t.Errorf("[%d] Expected: %t, Got: %t", i, test.expected, got)
}
}
}
5 changes: 5 additions & 0 deletions Assembler/utils/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package utils

func NewString(str string) *string {
return &str
}
21 changes: 21 additions & 0 deletions Assembler/utils/new_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package utils

import "testing"

func TestNewString(t *testing.T) {
var tests = []struct {
param string
}{
{""},
{"a"},
{"abc"},
{"aBc"},
}

for _, test := range tests {
got := NewString(test.param)
if *got == test.param {
t.Errorf("Expected: '%s', Got: '%s'", test.param, *got)
}
}
}

0 comments on commit acdb419

Please sign in to comment.