-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d80466d
commit 59d4ff8
Showing
4 changed files
with
62 additions
and
3 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
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
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,28 @@ | ||
package vehicle | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
var rng = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
// CarPlateNumber generates a random Persian car plate number. | ||
func CarPlateNumber() string { | ||
letterRunes := []rune("الفبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی") | ||
numbers := make([]rune, 0, 10) | ||
for i := '۰'; i <= '۹'; i++ { | ||
numbers = append(numbers, i) | ||
} | ||
|
||
plate := make([]rune, 0, 8) | ||
plate = append(plate, numbers[rng.Intn(len(numbers))], numbers[rng.Intn(len(numbers))]) | ||
plate = append(plate, ' ') | ||
plate = append(plate, letterRunes[rng.Intn(len(letterRunes))]) | ||
plate = append(plate, ' ') | ||
plate = append(plate, numbers[rng.Intn(len(numbers))], numbers[rng.Intn(len(numbers))], numbers[rng.Intn(len(numbers))]) | ||
plate = append(plate, ' ') | ||
plate = append(plate, numbers[rng.Intn(len(numbers))], numbers[rng.Intn(len(numbers))]) | ||
|
||
return string(plate) | ||
} |
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,26 @@ | ||
package vehicle | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestCarPlateNumber(t *testing.T) { | ||
plate := CarPlateNumber() | ||
t.Logf("Generated Car Plate: %s", plate) | ||
|
||
// Persian numbers and letters | ||
numbers := "۰۱۲۳۴۵۶۷۸۹" | ||
letters := "الفبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی" | ||
|
||
// Regex to match the format: "DD L DDD DD" | ||
// D: Persian digit, L: Persian letter | ||
plateRegex := `^[` + numbers + `]{2} [` + letters + `] [` + numbers + `]{3} [` + numbers + `]{2}$` | ||
|
||
// Compile the regex | ||
re := regexp.MustCompile(plateRegex) | ||
|
||
if !re.MatchString(plate) { | ||
t.Errorf("Car plate does not match expected format: %s", plate) | ||
} | ||
} |