Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Golang
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Muhammad, Hammad
Golang
Commits
771caf72
Commit
771caf72
authored
2 years ago
by
Muhammad, Hammad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added table test cases
parent
cd69af19
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
114 deletions
+80
-114
deck.go
deck.go
+0
-71
dect_test.go
dect_test.go
+0
-39
main.go
main.go
+32
-4
snake_case_test.go
snake_case_test.go
+48
-0
No files found.
deck.go
deleted
100644 → 0
View file @
cd69af19
package
main
import
(
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
)
// Create a new type of 'deck'
// which is a slice of strings
type
deck
[]
string
func
newDeck
()
deck
{
cards
:=
deck
{}
cardSuits
:=
[]
string
{
"Spades"
,
"Diamonds"
,
"Hearts"
,
"Clubs"
}
cardValues
:=
[]
string
{
"Ace"
,
"Two"
,
"Three"
,
"Four"
}
for
_
,
suit
:=
range
cardSuits
{
for
_
,
value
:=
range
cardValues
{
cards
=
append
(
cards
,
value
+
" of "
+
suit
)
}
}
return
cards
}
func
(
d
deck
)
print
()
{
for
i
,
card
:=
range
d
{
fmt
.
Println
(
i
,
card
)
}
}
func
deal
(
d
deck
,
handSize
int
)
(
deck
,
deck
)
{
return
d
[
:
handSize
],
d
[
handSize
:
]
}
func
(
d
deck
)
toString
()
string
{
return
strings
.
Join
([]
string
(
d
),
","
)
}
func
(
d
deck
)
saveToFile
(
filename
string
)
error
{
return
ioutil
.
WriteFile
(
filename
,
[]
byte
(
d
.
toString
()),
0666
)
}
func
newDeckFromFile
(
filename
string
)
deck
{
bs
,
err
:=
ioutil
.
ReadFile
(
filename
)
if
err
!=
nil
{
// Option #1 - log the error and return a call to newDeck()
// Option #2 - Log the error and entirely quit the program
fmt
.
Println
(
"Error:"
,
err
)
os
.
Exit
(
1
)
}
s
:=
strings
.
Split
(
string
(
bs
),
","
)
return
deck
(
s
)
}
func
(
d
deck
)
shuffle
()
{
source
:=
rand
.
NewSource
(
time
.
Now
()
.
UnixNano
())
r
:=
rand
.
New
(
source
)
for
i
:=
range
d
{
newPosition
:=
r
.
Intn
(
len
(
d
)
-
1
)
d
[
i
],
d
[
newPosition
]
=
d
[
newPosition
],
d
[
i
]
}
}
This diff is collapsed.
Click to expand it.
dect_test.go
deleted
100644 → 0
View file @
cd69af19
package
main
import
(
"os"
"testing"
)
func
TestNewDeck
(
t
*
testing
.
T
)
{
d
:=
newDeck
()
if
len
(
d
)
!=
16
{
t
.
Errorf
(
"Expected deck length of 16, but got %v"
,
len
(
d
))
}
if
d
[
0
]
!=
"Ace of Spades"
{
t
.
Errorf
(
"Expected first card of Ace of Spades, but got %v"
,
d
[
0
])
}
if
d
[
len
(
d
)
-
1
]
!=
"Four of Clubs"
{
t
.
Errorf
(
"Expected last card of Four of Clubs, but got %v"
,
d
[
len
(
d
)
-
1
])
}
}
func
TestSaveToDeckAndNewDeckFromFile
(
t
*
testing
.
T
)
{
os
.
Remove
(
"_decktesting"
)
deck
:=
newDeck
()
deck
.
saveToFile
(
"_decktesting"
)
loadedDeck
:=
newDeckFromFile
(
"_decktesting"
)
if
len
(
loadedDeck
)
!=
16
{
t
.
Errorf
(
"Expected 16 cards in deck, got %v"
,
len
(
loadedDeck
))
}
os
.
Remove
(
"_decktesting"
)
}
// Execute test file by this command : go test
This diff is collapsed.
Click to expand it.
main.go
View file @
771caf72
package
main
import
(
"fmt"
"strings"
)
func
main
()
{
cards
:=
newDeck
()
cards
.
shuffle
()
cards
.
print
()
str
:=
ToSnakeCase
(
"helloWorld !!!"
)
fmt
.
Println
(
str
)
}
// Execute commond both file like : go run main.go deck.go
func
ToSnakeCase
(
str
string
)
string
{
// step 0: handle empty string
if
str
==
""
{
return
""
}
// step 1: CamelCase to snake_case
for
i
:=
0
;
i
<
len
(
str
);
i
++
{
if
str
[
i
]
>=
'A'
&&
str
[
i
]
<=
'Z'
{
if
i
==
0
{
str
=
strings
.
ToLower
(
string
(
str
[
i
]))
+
str
[
i
+
1
:
]
}
else
{
str
=
str
[
:
i
]
+
"_"
+
strings
.
ToLower
(
string
(
str
[
i
]))
+
str
[
i
+
1
:
]
}
}
}
// step 2: remove spaces
str
=
strings
.
ReplaceAll
(
str
,
" "
,
""
)
// step 3: remove double-underscores
str
=
strings
.
ReplaceAll
(
str
,
"__"
,
"_"
)
return
str
}
This diff is collapsed.
Click to expand it.
snake_case_test.go
0 → 100644
View file @
771caf72
package
main
import
"testing"
func
TestToSnakeCase
(
t
*
testing
.
T
)
{
type
testCase
struct
{
description
string
input
string
expected
string
}
testCases
:=
[]
testCase
{
{
description
:
"empty string"
,
input
:
""
,
expected
:
""
,
},
{
description
:
"single word"
,
input
:
"Hello"
,
expected
:
"hello"
,
},
{
description
:
"two words (camel case)"
,
input
:
"HelloWorld"
,
expected
:
"hello_world"
,
},
{
description
:
"two words with space"
,
input
:
"Hello World"
,
expected
:
"hello_world"
,
},
{
description
:
"two words with space and underscore"
,
input
:
"Hello_World"
,
expected
:
"hello_world"
,
},
}
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
tc
.
description
,
func
(
t
*
testing
.
T
)
{
actual
:=
ToSnakeCase
(
tc
.
input
)
if
actual
!=
tc
.
expected
{
t
.
Errorf
(
"expected %s, got %s"
,
tc
.
expected
,
actual
)
}
})
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment