+19
@@ -0,0 +1,19 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func CreateWorkDir(t *testing.T) (string, func()) {
|
||||
t.Helper()
|
||||
dir, err := os.MkdirTemp("", "fs-test-workdir")
|
||||
if err != nil {
|
||||
t.Fatalf("create temp dir: %s", err)
|
||||
}
|
||||
return dir, func() {
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
t.Fatalf("remove temp dir: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
module galaxy/util
|
||||
|
||||
go 1.26.0
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package util
|
||||
|
||||
import "math"
|
||||
|
||||
func ShortDistance(w, h uint32, x1, y1, x2, y2 float64) float64 {
|
||||
return math.Hypot(deltas(w, h, x1, y1, x2, y2))
|
||||
}
|
||||
|
||||
func NextTravelCoord(w, h uint32, x1, y1, x2, y2, delta float64) (float64, float64, bool) {
|
||||
deltaX, deltaY := deltas(w, h, x1, y1, x2, y2)
|
||||
distance := math.Hypot(deltaX, deltaY)
|
||||
if distance <= delta {
|
||||
return x2, y2, true
|
||||
}
|
||||
// [ ] refactor - remove extra vars
|
||||
xa := 0.
|
||||
ya := 0.
|
||||
xb := deltaX
|
||||
yb := deltaY
|
||||
d := distance
|
||||
d2 := delta
|
||||
xc := xa - (d2*(xa-xb))/d
|
||||
yc := ya - (d2*(ya-yb))/d
|
||||
|
||||
// ---
|
||||
var tx, ty float64
|
||||
|
||||
if math.Abs(x2-x1) > float64(w/2) {
|
||||
// moving across X boundary
|
||||
if x2 < x1 {
|
||||
// moving across higher border
|
||||
tx = math.Mod(x1+xc, float64(w))
|
||||
} else {
|
||||
// moving across lower border
|
||||
tx = x1 - xc
|
||||
if tx < 0 {
|
||||
tx = float64(w) + tx
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if x2 < x1 {
|
||||
tx = x1 - xc
|
||||
} else {
|
||||
tx = x1 + xc
|
||||
}
|
||||
}
|
||||
|
||||
if math.Abs(y2-y1) > float64(h/2) {
|
||||
// moving across Y boundary
|
||||
if y2 < y1 {
|
||||
// moving across higher border
|
||||
ty = math.Mod(y1+yc, float64(h))
|
||||
} else {
|
||||
// moving across lower border
|
||||
ty = y1 - yc
|
||||
if ty < 0 {
|
||||
ty = float64(h) + ty
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if y2 < y1 {
|
||||
ty = y1 - yc
|
||||
} else {
|
||||
ty = y1 + yc
|
||||
}
|
||||
}
|
||||
|
||||
return tx, ty, false
|
||||
}
|
||||
|
||||
func deltas(w, h uint32, x1, y1, x2, y2 float64) (float64, float64) {
|
||||
dx := math.Abs(x2 - x1)
|
||||
dy := math.Abs(y2 - y1)
|
||||
if dx > float64(w/2) {
|
||||
dx = float64(h) - dx
|
||||
}
|
||||
if dy > float64(h/2) {
|
||||
dy = float64(h) - dy
|
||||
}
|
||||
return dx, dy
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package util_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"galaxy/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestShortDistance(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
w, h uint32
|
||||
x1, y1, x2, y2, d float64
|
||||
}{
|
||||
{10, 10, 0, 0, 5, 5, 7.071},
|
||||
{10, 10, 0, 0, 5.01, 5.01, 7.057},
|
||||
{10, 10, 2, 2, 8, 2, 4.},
|
||||
{10, 10, 8, 7, 1, 7, 3.},
|
||||
} {
|
||||
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
||||
d := util.ShortDistance(tc.w, tc.h, tc.x1, tc.y1, tc.x2, tc.y2)
|
||||
assert.Equal(t, tc.d, util.Fixed3(d))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNextTravelCoord(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
w, h uint32
|
||||
ox, oy, dx, dy, delta float64
|
||||
tx, ty float64
|
||||
arrived bool
|
||||
}{
|
||||
{w: 10, h: 10, ox: 0.0, oy: 0.0, dx: 2.0, dy: 0.0, delta: 1.0, tx: 1.0, ty: 0.0, arrived: false},
|
||||
{w: 10, h: 10, ox: 0.0, oy: 0.0, dx: 0.0, dy: 2.0, delta: 1.0, tx: 0.0, ty: 1.0, arrived: false},
|
||||
|
||||
{w: 10, h: 10, ox: 1.0, oy: 1.0, dx: 9.0, dy: 1.0, delta: 1.0, tx: 0.0, ty: 1.0, arrived: false},
|
||||
{w: 10, h: 10, ox: 1.0, oy: 9.5, dx: 1.0, dy: 1.0, delta: 1.0, tx: 1.0, ty: 0.5, arrived: false},
|
||||
|
||||
{w: 10, h: 10, ox: 1.0, oy: 1.0, dx: 5.0, dy: 5.0, delta: 2.0, tx: 2.414, ty: 2.414, arrived: false},
|
||||
{w: 10, h: 10, ox: 1.0, oy: 1.0, dx: 9.0, dy: 9.0, delta: 2.0, tx: 9.586, ty: 9.586, arrived: false},
|
||||
|
||||
{w: 10, h: 10, ox: 5.0, oy: 5.0, dx: 9.0, dy: 9.0, delta: 6.0, tx: 9.0, ty: 9.0, arrived: true},
|
||||
{w: 10, h: 10, ox: 6.0, oy: 6.0, dx: 10.0, dy: 10.0, delta: 6.0, tx: 10.0, ty: 10.0, arrived: true},
|
||||
{w: 10, h: 10, ox: 1.0, oy: 2.0, dx: 7.0, dy: 8.0, delta: 6.0, tx: 7.0, ty: 8.0, arrived: true},
|
||||
} {
|
||||
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
||||
tx, ty, arrived := util.NextTravelCoord(tc.w, tc.h, tc.ox, tc.oy, tc.dx, tc.dy, tc.delta)
|
||||
assert.Equal(t, tc.arrived, arrived)
|
||||
assert.Equal(t, tc.tx, util.Fixed3(tx))
|
||||
assert.Equal(t, tc.ty, util.Fixed3(ty))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
func Fixed3(num float64) float64 {
|
||||
return fixed(num, 3)
|
||||
}
|
||||
|
||||
func Fixed12(num float64) float64 {
|
||||
return fixed(num, 12)
|
||||
}
|
||||
|
||||
func fixed(num float64, precision int) float64 {
|
||||
output := math.Pow(10, float64(precision))
|
||||
return float64(round(num*output)) / output
|
||||
}
|
||||
|
||||
func round(num float64) int {
|
||||
return int(num + math.Copysign(0.5, num))
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFixed(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
precision int
|
||||
source, expected float64
|
||||
}{
|
||||
{3, 0, 0},
|
||||
{3, -1, -1},
|
||||
{3, 1.5, 1.5},
|
||||
{3, 2.25, 2.25},
|
||||
{3, 3.275, 3.275},
|
||||
{3, 4.0004, 4.000},
|
||||
{5, 5.000005, 5.00001},
|
||||
{4, -6.00004, -6.},
|
||||
{4, -6.00005, -6.0001},
|
||||
} {
|
||||
t.Run(fmt.Sprintf("%f", tc.source), func(t *testing.T) {
|
||||
if tc.precision == 3 {
|
||||
assert.Equal(t, tc.expected, Fixed3(tc.source))
|
||||
} else {
|
||||
assert.Equal(t, tc.expected, fixed(tc.source, tc.precision))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const (
|
||||
maxNameLength = 30
|
||||
specialChars = "!@#$%^*-_=+~()[]{}" // Allowed special characters
|
||||
)
|
||||
|
||||
var allowedSpecialChars map[rune]bool
|
||||
|
||||
func init() {
|
||||
allowedSpecialChars = make(map[rune]bool)
|
||||
for _, r := range []rune(specialChars) {
|
||||
allowedSpecialChars[r] = true
|
||||
}
|
||||
}
|
||||
|
||||
func ValidateTypeName(input string) (string, bool) {
|
||||
// Trim leading and trailing spaces
|
||||
trimmed := strings.TrimSpace(input)
|
||||
|
||||
// If the string is empty after trimming, return false
|
||||
if len(trimmed) == 0 {
|
||||
return "", false
|
||||
}
|
||||
|
||||
runes := []rune(trimmed)
|
||||
|
||||
if len(runes) > maxNameLength {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Dash cannot be at the beginning or end
|
||||
if allowedSpecialChars[runes[0]] || allowedSpecialChars[runes[len(runes)-1]] {
|
||||
return "", false
|
||||
}
|
||||
// if runes[0] == '-' || runes[len(runes)-1] == '-' {
|
||||
// return "", false
|
||||
// }
|
||||
|
||||
var specialCount uint8
|
||||
for _, r := range runes {
|
||||
// Check if the character is a whitespace, which is not allowed
|
||||
if unicode.IsSpace(r) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Letters (including any alphabet) and digits are allowed
|
||||
if unicode.IsLetter(r) || unicode.IsDigit(r) {
|
||||
specialCount = 0
|
||||
continue
|
||||
}
|
||||
|
||||
// Combining marks (accents) are allowed
|
||||
if unicode.IsMark(r) {
|
||||
specialCount = 0
|
||||
continue
|
||||
}
|
||||
|
||||
// Check for allowed special characters
|
||||
if allowedSpecialChars[r] {
|
||||
if specialCount == 2 {
|
||||
return "", false
|
||||
}
|
||||
specialCount++
|
||||
continue
|
||||
}
|
||||
|
||||
// If any other character is encountered, return false
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Return the trimmed string and true if all conditions are met
|
||||
return trimmed, true
|
||||
}
|
||||
|
||||
func AppendRandomSuffix(v string) string {
|
||||
return AppendRandomSuffixGenerator(v, RandomSuffixGenerator)
|
||||
}
|
||||
|
||||
func AppendRandomSuffixGenerator(v string, s func() string) string {
|
||||
suffix := []rune(s())
|
||||
str := []rune(v)
|
||||
max := maxNameLength - len(suffix)
|
||||
if len(str) > max {
|
||||
str = str[:max]
|
||||
}
|
||||
return string(append(str, suffix...))
|
||||
}
|
||||
|
||||
func RandomSuffixGenerator() string {
|
||||
return fmt.Sprintf("%04d", rand.Intn(9999))
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
package util_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
|
||||
"galaxy/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateString(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
ok bool
|
||||
}{
|
||||
// Basic cases
|
||||
{
|
||||
name: "Valid string with Latin characters and digits",
|
||||
input: "Hello_World-123",
|
||||
expected: "Hello_World-123",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Valid string with Cyrillic characters",
|
||||
input: "Привет_мир-42",
|
||||
expected: "Привет_мир-42",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Valid Greek alphabet string",
|
||||
input: "Αλφα_Βητα-2024",
|
||||
expected: "Αλφα_Βητα-2024",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Valid Arabic alphabet string",
|
||||
input: "مرحبا_العالم-7",
|
||||
expected: "مرحبا_العالم-7",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Valid Japanese Katakana string",
|
||||
input: "テスト_ケース-1",
|
||||
expected: "テスト_ケース-1",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Valid Chinese characters",
|
||||
input: "你好_世界-123", // "Hello World" in Chinese
|
||||
expected: "你好_世界-123",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Valid Hindi characters",
|
||||
input: "नमस्ते_दुनिया-456", // "Hello World" in Hindi
|
||||
expected: "नमस्ते_दुनिया-456",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Valid Thai characters",
|
||||
input: "สวัสดี_โลก-789", // "Hello World" in Thai
|
||||
expected: "สวัสดี_โลก-789",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Valid Korean characters",
|
||||
input: "안녕하세요_세계-101", // "Hello World" in Korean
|
||||
expected: "안녕하세요_세계-101",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Valid Hebrew characters",
|
||||
input: "שלום_עולם-202", // "Hello World" in Hebrew
|
||||
expected: "שלום_עולם-202",
|
||||
ok: true,
|
||||
},
|
||||
// Special characters test cases
|
||||
{
|
||||
name: "Valid special character @",
|
||||
input: "Test@Name",
|
||||
expected: "Test@Name",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Valid special character ^",
|
||||
input: "Test^Name",
|
||||
expected: "Test^Name",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Valid special character ~",
|
||||
input: "Test~Name",
|
||||
expected: "Test~Name",
|
||||
ok: true,
|
||||
},
|
||||
// Edge cases
|
||||
{
|
||||
name: "Spaces are trimmed from both ends",
|
||||
input: " Test123_Name ",
|
||||
expected: "Test123_Name",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Spaces in the middle are not allowed",
|
||||
input: "Test 123",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "Tab character in the middle is not allowed",
|
||||
input: "Test\tName",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "Newline character is not allowed",
|
||||
input: "Test\nName",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "Dash at the beginning after TrimSpace is not allowed",
|
||||
input: " -Test123",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "Dash at the end after TrimSpace is not allowed",
|
||||
input: "Test123- ",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "Emoji is not allowed",
|
||||
input: "Test🙂Name",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "String containing only spaces",
|
||||
input: " ",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "Empty string",
|
||||
input: "",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "Too long string",
|
||||
input: "ValidatedStringHasTooManyCharacters",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "Valid consecutive special chars",
|
||||
input: "Valid_(special)_Chars",
|
||||
expected: "Valid_(special)_Chars",
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "Too many consecutive special chars",
|
||||
input: "Too_Many_(special[_]Chars",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "Special char at the beginning",
|
||||
input: "$pecialString",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "Special char at the end",
|
||||
input: "SpecialString_",
|
||||
expected: "",
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "All valid special chars",
|
||||
input: "A@#b$%c^*d-_e=+f~(g)[h]{i}j",
|
||||
expected: "A@#b$%c^*d-_e=+f~(g)[h]{i}j",
|
||||
ok: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, ok := util.ValidateTypeName(tt.input)
|
||||
assert.Equal(t, tt.ok, ok)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Fuzz test for ValidateString function
|
||||
func FuzzValidateString(f *testing.F) {
|
||||
// Adding a few basic strings to start the fuzz test
|
||||
f.Add("Hello_World-123")
|
||||
f.Add("Test@Name")
|
||||
f.Add("Привет_мир-42")
|
||||
f.Add("αβγ@~")
|
||||
f.Add("مرحبا_العالم-7")
|
||||
|
||||
// Fuzz function
|
||||
f.Fuzz(func(t *testing.T, input string) {
|
||||
// Call the function and check if the result matches expectations
|
||||
result, ok := util.ValidateTypeName(input)
|
||||
|
||||
// Check if the string is non-empty and valid UTF-8
|
||||
if len(input) > 0 {
|
||||
if !utf8.ValidString(input) {
|
||||
t.Errorf("Error: string is not a valid UTF-8 string: %s", input)
|
||||
}
|
||||
}
|
||||
|
||||
// If the string is empty, ok should be false
|
||||
if len(result) == 0 {
|
||||
if ok {
|
||||
t.Errorf("Expected false for invalid string, but got true: %s", input)
|
||||
}
|
||||
} else {
|
||||
// If the result is not empty, ok should be true
|
||||
if !ok {
|
||||
t.Errorf("Expected true for valid string, but got false: %s", input)
|
||||
}
|
||||
}
|
||||
|
||||
// Additional check: if input has spaces at the beginning or end, it should fail
|
||||
if input[0] == ' ' || input[len(input)-1] == ' ' {
|
||||
if ok {
|
||||
t.Errorf("Error: string contains spaces at the beginning or end: %s", input)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAppendRandomSuffixGenerator(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
suffix string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "Regular String",
|
||||
input: "Regular_String",
|
||||
suffix: "1234",
|
||||
expected: "Regular_String1234",
|
||||
},
|
||||
{
|
||||
name: "Zero Length String",
|
||||
input: "",
|
||||
suffix: "1234",
|
||||
expected: "1234",
|
||||
},
|
||||
{
|
||||
name: "Edge Case String len=28",
|
||||
input: "Edge_Case_String_ABCDEFGHIGK",
|
||||
suffix: "1234",
|
||||
expected: "Edge_Case_String_ABCDEFGHI1234",
|
||||
},
|
||||
{
|
||||
name: "Extra Long String len=31",
|
||||
input: "Extra_Long_String_ABCDEFGHIGKLM",
|
||||
suffix: "1234",
|
||||
expected: "Extra_Long_String_ABCDEFGH1234",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := util.AppendRandomSuffixGenerator(tt.input, func() string { return tt.suffix })
|
||||
assert.Equal(t, tt.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomSuffixGenerator(t *testing.T) {
|
||||
var last string
|
||||
for range 100 {
|
||||
s := util.RandomSuffixGenerator()
|
||||
assert.Len(t, s, 4)
|
||||
assert.NotEqual(t, last, s)
|
||||
assert.True(t, strings.ContainsFunc(s, func(r rune) bool { return r >= '0' && r <= '9' }))
|
||||
last = s
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user