From b1645938f27b7300d395dd97b428ed309da0d3fa Mon Sep 17 00:00:00 2001 From: SIDANWhatever Date: Tue, 17 Dec 2024 13:56:21 +0800 Subject: [PATCH] feat: add gep to value --- models/value.go | 9 ++++ models/value_test.go | 116 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/models/value.go b/models/value.go index c1f09ef..545f7b8 100644 --- a/models/value.go +++ b/models/value.go @@ -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) diff --git a/models/value_test.go b/models/value_test.go index bc3bc54..816a1b9 100644 --- a/models/value_test.go +++ b/models/value_test.go @@ -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) + } + }) + } +}