Skip to content

Commit

Permalink
Merge pull request #2 from pankaj061999/Build_issue_Fix
Browse files Browse the repository at this point in the history
Create test file and write test fucn and shuffling random func
  • Loading branch information
pankaj061999 authored Feb 1, 2024
2 parents 1f69673 + 55bd1a4 commit cc47114
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 3 deletions.
42 changes: 40 additions & 2 deletions Cards/deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
66 changes: 66 additions & 0 deletions Cards/deck_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
}
})
}
}
3 changes: 3 additions & 0 deletions Cards/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module cards

go 1.21.6
18 changes: 18 additions & 0 deletions Cards/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion Cards/pankaj.txt
Original file line number Diff line number Diff line change
@@ -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
Pankaj of Jaipur,Pankaj of Bundi,suri of Jaipur,suri of Bundi

0 comments on commit cc47114

Please sign in to comment.