Skip to content

Commit

Permalink
feat: add gep to value
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Dec 17, 2024
1 parent 57eecef commit b164593
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
9 changes: 9 additions & 0 deletions models/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ func (mv *Value) ToAssets() *[]Asset {
return &assets
}

func (mv *Value) Geq(other *Value) bool {
for unit, quantity := range other.Value {
if existingQuantity, exists := mv.Value[unit]; !exists || existingQuantity < quantity {
return false
}
}
return true
}

func FromAssets(assets []Asset) *Value {
mv := NewValue()
mv.AddAssets(assets)
Expand Down
116 changes: 116 additions & 0 deletions models/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,119 @@ func TestFromAssets(t *testing.T) {
t.Errorf("Expected 200, got %d", mv.Get("EUR"))
}
}

func TestGeq(t *testing.T) {
// Define test cases
tests := []struct {
name string
value1 *Value
value2 *Value
expected bool
}{
{
name: "Value1 greater than or equal to Value2",
value1: &Value{
Value: map[string]int64{
"asset1": 1000,
"asset2": 2000,
},
},
value2: &Value{
Value: map[string]int64{
"asset1": 500,
"asset2": 1500,
},
},
expected: true,
},
{
name: "Value1 less than Value2",
value1: &Value{
Value: map[string]int64{
"asset1": 1000,
"asset2": 1000,
},
},
value2: &Value{
Value: map[string]int64{
"asset1": 1500,
"asset2": 1500,
},
},
expected: false,
},
{
name: "Value1 equal to Value2",
value1: &Value{
Value: map[string]int64{
"asset1": 1000,
"asset2": 2000,
},
},
value2: &Value{
Value: map[string]int64{
"asset1": 1000,
"asset2": 2000,
},
},
expected: true,
},
{
name: "Value1 has more assets than Value2",
value1: &Value{
Value: map[string]int64{
"asset1": 1000,
"asset2": 2000,
"asset3": 3000,
},
},
value2: &Value{
Value: map[string]int64{
"asset1": 1000,
"asset2": 2000,
},
},
expected: true,
},
{
name: "Value1 has fewer assets than Value2",
value1: &Value{
Value: map[string]int64{
"asset1": 1000,
},
},
value2: &Value{
Value: map[string]int64{
"asset1": 1000,
"asset2": 2000,
},
},
expected: false,
},
{
name: "Value1 has fewer assets than Value2",
value1: &Value{
Value: map[string]int64{
"asset1": 1500,
"asset2": 1500,
},
},
value2: &Value{
Value: map[string]int64{
"asset1": 1000,
"asset2": 2000,
},
},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.value1.Geq(tt.value2)
if result != tt.expected {
t.Errorf("Geq() = %v, want %v", result, tt.expected)
}
})
}
}

0 comments on commit b164593

Please sign in to comment.