|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "path" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/vhive-serverless/loader/tools/multi_loader/types" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNextProduct(t *testing.T) { |
| 13 | + ints := []int{2, 1} |
| 14 | + nextProduct := NextCProduct(ints) |
| 15 | + expectedArrs := [][]int{{0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 0}, {2, 1}} |
| 16 | + curI := 0 |
| 17 | + |
| 18 | + for { |
| 19 | + product := nextProduct() |
| 20 | + if len(product) == 0 { |
| 21 | + if curI != len(expectedArrs) { |
| 22 | + t.Fatalf("Expected %d products, got %d", len(expectedArrs), curI) |
| 23 | + } |
| 24 | + break |
| 25 | + } |
| 26 | + if len(product) != len(ints) { |
| 27 | + t.Fatalf("Expected product length %d, got %d", len(ints), len(product)) |
| 28 | + } |
| 29 | + for i, v := range product { |
| 30 | + if v != expectedArrs[curI][i] { |
| 31 | + t.Fatalf("Expected %v, got %v", expectedArrs[curI], product) |
| 32 | + } |
| 33 | + } |
| 34 | + curI++ |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestSplitPath(t *testing.T) { |
| 39 | + assert.Equal(t, []string{"file.txt"}, SplitPath("file.txt"), "Expected ['file.txt'] for single file") |
| 40 | + assert.Equal(t, []string{"home", "user", "docs", "file.txt"}, SplitPath(filepath.Join("home", "user", "docs", "file.txt")), "Expected full path split") |
| 41 | +} |
| 42 | + |
| 43 | +func TestSweepOptionsToPostfix(t *testing.T) { |
| 44 | + t.Run("Test Post Fix Naming Util", func(t *testing.T) { |
| 45 | + result := SweepOptionsToPostfix( |
| 46 | + []types.SweepOptions{ |
| 47 | + {Field: "PreScript", Values: []interface{}{"PreValue_1", "PreValue_2"}}, |
| 48 | + {Field: "CPULimit", Values: []interface{}{"1vCPU", "2vCPU", "4vCPU"}}, |
| 49 | + {Field: "ExperimentDuration", Values: []interface{}{"10", "20", "30"}}, |
| 50 | + {Field: "PostScript", Values: []interface{}{"PostValue_1", "PostValue_2", "PostValue_3"}}, |
| 51 | + }, |
| 52 | + []int{1, 2, 0, 2}, |
| 53 | + ) |
| 54 | + assert.Equal(t, "_PreScript_PreValue_2_CPULimit_4vCPU_ExperimentDuration_10_PostScript_PostValue_3", result, "Unexpected postfix result") |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func TestUpdateExperimentWithSweepIndices(t *testing.T) { |
| 59 | + experiment := &types.LoaderExperiment{ |
| 60 | + Name: "test_experiment", |
| 61 | + Config: map[string]interface{}{"OutputPathPrefix": "first/second/third"}, |
| 62 | + } |
| 63 | + |
| 64 | + // Test sweep options |
| 65 | + sweepOptions := []types.SweepOptions{ |
| 66 | + {Field: "PreScript", Values: []interface{}{"pre1", "pre2"}, Format: "test_{}"}, |
| 67 | + {Field: "PostScript", Values: []interface{}{"post1", "post2"}, Format: ""}, |
| 68 | + {Field: "ExperimentDuration", Values: []interface{}{"1", "2"}, Format: ""}, |
| 69 | + } |
| 70 | + |
| 71 | + selectedSweepValues := []int{1, 0, 1} |
| 72 | + |
| 73 | + UpdateExperimentWithSweepIndices(experiment, sweepOptions, selectedSweepValues) |
| 74 | + |
| 75 | + expectedPostfix := "_PreScript_pre2_PostScript_post1_ExperimentDuration_2" |
| 76 | + |
| 77 | + // Check experiment name |
| 78 | + assert.Equal(t, "test_experiment"+expectedPostfix, experiment.Name) |
| 79 | + |
| 80 | + // Check OutputPathPrefix |
| 81 | + expectedOutputPathPrefix := path.Join("first", "second"+expectedPostfix, "third") |
| 82 | + assert.Equal(t, expectedOutputPathPrefix, experiment.Config["OutputPathPrefix"]) |
| 83 | + |
| 84 | + // Verify that the sweep options have been updated |
| 85 | + assert.Equal(t, "test_pre2", experiment.PreScript) |
| 86 | + assert.Equal(t, "post1", experiment.PostScript) |
| 87 | + assert.Equal(t, "2", experiment.Config["ExperimentDuration"]) |
| 88 | +} |
0 commit comments