|
| 1 | +package util |
| 2 | + |
| 3 | +import "testing" |
| 4 | +import . "github.com/onsi/gomega" |
| 5 | + |
| 6 | +func TestMemorySize_Set(t *testing.T) { |
| 7 | + RegisterTestingT(t) // Register Gomega for the test |
| 8 | + |
| 9 | + t.Run("valid inputs", func(t *testing.T) { |
| 10 | + var ms MemorySize |
| 11 | + |
| 12 | + // Test inputs with valid values |
| 13 | + err := ms.Set("128KB") |
| 14 | + Expect(err).To(BeNil()) |
| 15 | + Expect(ms).To(Equal(MemorySize(128 * 1024))) |
| 16 | + |
| 17 | + err = ms.Set("2MB") |
| 18 | + Expect(err).To(BeNil()) |
| 19 | + Expect(ms).To(Equal(MemorySize(2 * 1024 * 1024))) |
| 20 | + |
| 21 | + err = ms.Set("1GB") |
| 22 | + Expect(err).To(BeNil()) |
| 23 | + Expect(ms).To(Equal(MemorySize(1 * 1024 * 1024 * 1024))) |
| 24 | + |
| 25 | + err = ms.Set("1024") // No suffix, treat as bytes |
| 26 | + Expect(err).To(BeNil()) |
| 27 | + Expect(ms).To(Equal(MemorySize(1024))) |
| 28 | + |
| 29 | + err = ms.Set(" 64MB ") // Test with leading/trailing spaces |
| 30 | + Expect(err).To(BeNil()) |
| 31 | + Expect(ms).To(Equal(MemorySize(64 * 1024 * 1024))) |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("invalid inputs", func(t *testing.T) { |
| 35 | + var ms MemorySize |
| 36 | + |
| 37 | + // Test inputs with invalid values |
| 38 | + Expect(ms.Set("10XYZ")).To(Not(BeNil())) // Unknown unit |
| 39 | + Expect(ms.Set("ABC")).To(Not(BeNil())) // Non-numeric input |
| 40 | + Expect(ms.Set("")).To(Not(BeNil())) // Empty input |
| 41 | + Expect(ms.Set("-5MB")).To(Not(BeNil())) // Negative value |
| 42 | + }) |
| 43 | + |
| 44 | + t.Run("boundary cases", func(t *testing.T) { |
| 45 | + var ms MemorySize |
| 46 | + |
| 47 | + // Test extremely large numbers |
| 48 | + err := ms.Set("1099511627776GB") // 1 PB (petabyte, very large) |
| 49 | + Expect(err).To(BeNil()) |
| 50 | + |
| 51 | + // Overflow handling (in practice, you'd want to handle overflow explicitly) |
| 52 | + err = ms.Set("9223372036854775808") // Larger than int64 max |
| 53 | + Expect(err).To(Not(BeNil())) |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +func TestMemorySize_AsBytes(t *testing.T) { |
| 58 | + RegisterTestingT(t) // Register Gomega for the test |
| 59 | + |
| 60 | + var ms MemorySize |
| 61 | + |
| 62 | + // Set a value and check its string representation |
| 63 | + ms = 128 * 1024 |
| 64 | + Expect(ms.ToBytes()).To(Equal(int64(131072))) |
| 65 | + |
| 66 | + ms = 2 * 1024 * 1024 |
| 67 | + Expect(ms.ToBytes()).To(Equal(int64(2097152))) |
| 68 | +} |
0 commit comments