refactor: move packages to pkg directory
This commit is contained in:
		
							
								
								
									
										12
									
								
								pkg/util/util.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								pkg/util/util.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
package util
 | 
			
		||||
 | 
			
		||||
// MapRange Linear mapping between two ranges of values
 | 
			
		||||
func MapRange(x, xmin, xmax, ymin, ymax float64) int {
 | 
			
		||||
	Xrange := xmax - xmin
 | 
			
		||||
	Yrange := ymax - ymin
 | 
			
		||||
	XYratio := Xrange / Yrange
 | 
			
		||||
 | 
			
		||||
	y := (x-xmin)/XYratio + ymin
 | 
			
		||||
 | 
			
		||||
	return int(y)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								pkg/util/util_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								pkg/util/util_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
package util
 | 
			
		||||
 | 
			
		||||
import "testing"
 | 
			
		||||
 | 
			
		||||
func TestRangeMapping(t *testing.T) {
 | 
			
		||||
	cases := []struct {
 | 
			
		||||
		x, xmin, xmax, ymin, ymax float64
 | 
			
		||||
		expected                  int
 | 
			
		||||
	}{
 | 
			
		||||
		// Test positive
 | 
			
		||||
		{-100, -100, 100, 0, 1000, 0},
 | 
			
		||||
		{0, -100, 100, 0, 1000, 500},
 | 
			
		||||
		{100, -100, 100, 0, 1000, 1000},
 | 
			
		||||
 | 
			
		||||
		// Test negative
 | 
			
		||||
		{0, 0, 100, 0, 1000, 0},
 | 
			
		||||
		{50, 0, 100, 0, 1000, 500},
 | 
			
		||||
		{100, 0, 100, 0, 1000, 1000},
 | 
			
		||||
 | 
			
		||||
		// Reverse
 | 
			
		||||
		{0, 100, 0, 0, 1000, 1000},
 | 
			
		||||
		{50, 100, 0, 0, 1000, 500},
 | 
			
		||||
		{100, 100, 0, 0, 1000, 0},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, c := range cases {
 | 
			
		||||
		val := MapRange(c.x, c.xmin, c.xmax, c.ymin, c.ymax)
 | 
			
		||||
		if val != c.expected {
 | 
			
		||||
			t.Errorf("MapRange(%.0f, %.0f, %.0f, %.0f, %.0f): %d, wants %d", c.x, c.xmin, c.xmax, c.ymin, c.ymax, val, c.expected)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user