From 55bd1a46641e86170b6dfd7d1143fc6dec1f16f7 Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Meena Date: Thu, 1 Feb 2024 12:59:21 +0530 Subject: [PATCH] Create test file and write test fucn and shuffling random func --- Cards/deck.go | 42 +++++++++++++++++++++++++++-- Cards/deck_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++ Cards/go.mod | 3 +++ Cards/main.go | 18 +++++++++++++ Cards/pankaj.txt | 2 +- 5 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 Cards/deck_test.go create mode 100644 Cards/go.mod diff --git a/Cards/deck.go b/Cards/deck.go index a15f86e..fed0cb9 100644 --- a/Cards/deck.go +++ b/Cards/deck.go @@ -2,12 +2,15 @@ package main import ( "fmt" + "math/rand" "os" "strings" + "time" ) // Define a type type Person []string +type ResultNuber []int64 // Method with a value receiver func (p Person) PrintPerson() { @@ -23,8 +26,8 @@ func (p Person) PrintPerson() { func NewDeckFunc() Person { Cards := Person{} - cardsSuits := []string{"Pankaj", "suri", "Arjun", "Meena"} - cardsSet := []string{"Jaipur", "Bundi", "New Delhi", "Roorkee"} + cardsSuits := []string{"Pankaj", "suri"} + cardsSet := []string{"Jaipur", "Bundi"} for _, suits := range cardsSuits { for _, setsValue := range cardsSet { @@ -67,3 +70,38 @@ func ReadStringFile(fileName string) Person { return Person(s) } + +// shuffiing fucntion +func (p Person) shuffiingFunc() { + for i := range p { + newPostion := rand.Intn(len(p) - 1) + p[i], p[newPostion] = p[newPostion], p[i] + } +} + +// random nuber genrate + +func (p Person) randomGenrateFunc() { + newRandom := rand.NewSource(time.Now().UnixNano()) + r := rand.New(newRandom) + fmt.Println(r) + + for i := range p { + newPostion := r.Intn(len(p) - 1) + p[i], p[newPostion] = p[newPostion], p[i] + } + +} + +func (r ResultNuber) checkEvenOrOddNuber() []string { + var result []string + for _, num := range r { + if num%2 == 0 { + result = append(result, fmt.Sprintf("%d is even", num)) + } else { + result = append(result, fmt.Sprintf("%d is odd", num)) + } + } + + return result +} diff --git a/Cards/deck_test.go b/Cards/deck_test.go new file mode 100644 index 0000000..d86f99d --- /dev/null +++ b/Cards/deck_test.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "os" + "testing" +) + +func TestDeckFileNewDeckFunction(t *testing.T) { + + d := NewDeckFunc() + + if len(d) != 4 { + t.Errorf("Expected length 4, but got %d", len(d)) + } + + if d[0] != "Pankaj of Jaipur" { + t.Errorf("Expected d[0] value is Pankaj , But got %s ", d[0]) + } +} + +func TestNewDecFileCreateChec(t *testing.T) { + os.Remove("_DeckFile") + + deck := NewDeckFunc() + deck.SavedFile("_DeckFile") + loadDeckFile := ReadStringFile("_DeckFile") + + if len(loadDeckFile) != 13 { + t.Errorf("Expectd File Length 13 but Expected %v", len(loadDeckFile)) + } + + os.Remove("_DeckFile") + +} + +// check even and odd nuber +func TestCheckEvenOrOddNumber(t *testing.T) { + tests := []struct { + input ResultNuber + output []string + }{ + { + input: ResultNuber{2, 7, 4, 9, 6}, + output: []string{"2 is even", "7 is odd", "4 is even", "9 is odd", "6 is even"}, + }, + // Add more test cases as needed + } + + for _, test := range tests { + t.Run(fmt.Sprintf("Input: %v", test.input), func(t *testing.T) { + result := test.input.checkEvenOrOddNuber() + + if len(result) != len(test.output) { + t.Errorf("Expected %d messages, but got %d", len(test.output), len(result)) + return + } + + for i, expectedMessage := range test.output { + if result[i] != expectedMessage { + t.Errorf("Expected: %s, Got: %s", expectedMessage, result[i]) + } + } + }) + } +} diff --git a/Cards/go.mod b/Cards/go.mod new file mode 100644 index 0000000..13f3798 --- /dev/null +++ b/Cards/go.mod @@ -0,0 +1,3 @@ +module cards + +go 1.21.6 diff --git a/Cards/main.go b/Cards/main.go index a4ab461..352c218 100644 --- a/Cards/main.go +++ b/Cards/main.go @@ -89,4 +89,22 @@ func main() { fmt.Println("Read File Value ==>>", readFile) + // shuffling name + + resultShuffling := NewDeckFunc() + resultShuffling.shuffiingFunc() + resultShuffling.PrintPerson() + + // return random nuber + resultShuffling.randomGenrateFunc() + resultShuffling.PrintPerson() + + // check slice nuber even or odd + + numbers := ResultNuber{2, 7, 4, 9, 6} + messages := numbers.checkEvenOrOddNuber() + + for _, message := range messages { + fmt.Println(message) + } } diff --git a/Cards/pankaj.txt b/Cards/pankaj.txt index afc6985..d15ef38 100644 --- a/Cards/pankaj.txt +++ b/Cards/pankaj.txt @@ -1 +1 @@ -Pankaj of Jaipur,Pankaj of Bundi,Pankaj of New Delhi,Pankaj of Roorkee,suri of Jaipur,suri of Bundi,suri of New Delhi,suri of Roorkee,Arjun of Jaipur,Arjun of Bundi,Arjun of New Delhi,Arjun of Roorkee,Meena of Jaipur,Meena of Bundi,Meena of New Delhi,Meena of Roorkee \ No newline at end of file +Pankaj of Jaipur,Pankaj of Bundi,suri of Jaipur,suri of Bundi \ No newline at end of file