-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from new-aspect/my-new-feature
feat: Add tests for parsing CPU, memory and time duration
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/pbnjay/memory" | ||
"runtime" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestParseEatCPUCount(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected float64 | ||
}{ | ||
{"100%", float64(runtime.NumCPU())}, | ||
{"50%", float64(runtime.NumCPU()) * 0.5}, | ||
{"1", 1.0}, | ||
{"2", 2.0}, | ||
{"invalid", 0.0}, // Invalid input should return 0 | ||
} | ||
|
||
for _, tt := range tests { | ||
result := parseEatCPUCount(tt.input) | ||
if result != tt.expected { | ||
t.Errorf("Expected %f for input %s, got %f", tt.expected, tt.input, result) | ||
} | ||
} | ||
} | ||
|
||
func TestParseEatMemoryBytes(t *testing.T) { | ||
totalMemory := memory.TotalMemory() | ||
|
||
tests := []struct { | ||
input string | ||
expect uint64 | ||
}{ | ||
{"100%", totalMemory}, | ||
{"50%", totalMemory / 2}, | ||
{"1G", 1 * 1024 * 1024 * 1024}, | ||
{"1M", 1 * 1024 * 1024}, | ||
{"1K", 1 * 1024}, | ||
{"invalid", 0}, // invalid input should return 0 | ||
} | ||
|
||
for _, tt := range tests { | ||
result := parseEatMemoryBytes(tt.input) | ||
if result != tt.expect { | ||
t.Errorf("Expected %d for input %s, got %d", tt.expect, tt.input, result) | ||
} | ||
} | ||
} | ||
|
||
func TestParesTimeDuration(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected time.Duration | ||
}{ | ||
{"1s", 1 * time.Second}, | ||
{"1m", 1 * time.Minute}, | ||
{"1h", 1 * time.Hour}, | ||
{"invalid", 0}, // Invalid input should return 0 | ||
{"-1s", 0}, // Negative duration should return 0 | ||
} | ||
|
||
for _, tt := range tests { | ||
result := parseTimeDuration(tt.input) | ||
if result != tt.expected { | ||
t.Errorf("Expected %d for input %s, got %d", tt.expected, tt.input, result) | ||
} | ||
} | ||
} |