Skip to content

Commit bd8e6d5

Browse files
tjgurwara99github-action
and
github-action
authored
Fix: restructure checksum package (TheAlgorithms#485)
* fix: [generic + sort] All ready for generics * GoDocMD support for Go 1.18 * Bump Go version in workflow * Updated Documentation in README.md * bump go version in GoLangCI lint action * fix: [generic + sort] review feedback * Updated Documentation in README.md * empty commit: stuck github action * fix: restructuring checksum package * Updated Documentation in README.md Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent f15028b commit bd8e6d5

File tree

6 files changed

+97
-103
lines changed

6 files changed

+97
-103
lines changed

README.md

+14-25
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,14 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
162162
---
163163
##### Functions:
164164

165-
1. [`LuhnAlgorithm`](./checksum/luhn.go#L11): LuhnAlgorithm This function calculates the checksum using the Luna algorithm
165+
1. [`CRC8`](./checksum/crc8.go#L25): CRC8 calculates CRC8 checksum of the given data.
166+
2. [`Luhn`](./checksum/luhn.go#L11): Luhn validates the provided data using the Luhn algorithm.
167+
168+
---
169+
##### Types
170+
171+
1. [`CRCModel`](./checksum/crc8.go#L15): No description provided.
172+
166173

167174
---
168175
</details><details>
@@ -217,30 +224,12 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
217224
2. [`Base64Encode`](./conversion/base64.go#L19): Base64Encode encodes the received input bytes slice into a base64 string. The implementation follows the RFC4648 standard, which is documented at https://datatracker.ietf.org/doc/html/rfc4648#section-4
218225
3. [`BinaryToDecimal`](./conversion/binarytodecimal.go#L25): BinaryToDecimal() function that will take Binary number as string, and return it's Decimal equivalent as integer.
219226
4. [`DecimalToBinary`](./conversion/decimaltobinary.go#L32): DecimalToBinary() function that will take Decimal number as int, and return it's Binary equivalent as string.
220-
5. [`HEXToRGB`](./conversion/rgbhex.go#L10): HEXToRGB splits an RGB input (e.g. a color in hex format; 0x<color-code>) into the individual components: red, green and blue
221-
6. [`IntToRoman`](./conversion/integertoroman.go#L17): IntToRoman converts an integer value to a roman numeral string. An error is returned if the integer is not between 1 and 3999.
222-
7. [`RGBToHEX`](./conversion/rgbhex.go#L41): RGBToHEX does exactly the opposite of HEXToRGB: it combines the three components red, green and blue to an RGB value, which can be converted to e.g. Hex
223-
8. [`Reverse`](./conversion/decimaltobinary.go#L22): Reverse() function that will take string, and returns the reverse of that string.
224-
9. [`RomanToInteger`](./conversion/romantointeger.go#L40): RomanToInteger converts a roman numeral string to an integer. Roman numerals for numbers outside the range 1 to 3,999 will return an error. Nil or empty string return 0 with no error thrown.
225-
226-
---
227-
</details><details>
228-
<summary> <strong> crc </strong> </summary>
229-
230-
---
231-
232-
##### Package crc describes algorithms for finding various CRC checksums
233-
234-
---
235-
##### Functions:
236-
237-
1. [`CalculateCRC8`](./checksum/crc/crc8.go#L26): CalculateCRC8 This function calculate CRC8 checksum.
238-
239-
---
240-
##### Types
241-
242-
1. [`CRCModel`](./checksum/crc/crc8.go#L16): No description provided.
243-
227+
5. [`FuzzBase64Encode`](./conversion/base64_test.go#L113): No description provided.
228+
6. [`HEXToRGB`](./conversion/rgbhex.go#L10): HEXToRGB splits an RGB input (e.g. a color in hex format; 0x<color-code>) into the individual components: red, green and blue
229+
7. [`IntToRoman`](./conversion/integertoroman.go#L17): IntToRoman converts an integer value to a roman numeral string. An error is returned if the integer is not between 1 and 3999.
230+
8. [`RGBToHEX`](./conversion/rgbhex.go#L41): RGBToHEX does exactly the opposite of HEXToRGB: it combines the three components red, green and blue to an RGB value, which can be converted to e.g. Hex
231+
9. [`Reverse`](./conversion/decimaltobinary.go#L22): Reverse() function that will take string, and returns the reverse of that string.
232+
10. [`RomanToInteger`](./conversion/romantointeger.go#L40): RomanToInteger converts a roman numeral string to an integer. Roman numerals for numbers outside the range 1 to 3,999 will return an error. Nil or empty string return 0 with no error thrown.
244233

245234
---
246235
</details><details>

checksum/crc/crc8_test.go

-59
This file was deleted.

checksum/crc/crc8.go renamed to checksum/crc8.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
// author(s) [red_byte](https://github.com/i-redbyte)
88
// see crc8_test.go
99

10-
// Package crc describes algorithms for finding various CRC checksums
11-
package crc
10+
package checksum
1211

1312
import "math/bits"
1413

@@ -22,8 +21,8 @@ type CRCModel struct {
2221
Name string
2322
}
2423

25-
// CalculateCRC8 This function calculate CRC8 checksum.
26-
func CalculateCRC8(data []byte, model CRCModel) uint8 {
24+
// CRC8 calculates CRC8 checksum of the given data.
25+
func CRC8(data []byte, model CRCModel) uint8 {
2726
table := getTable(model)
2827
crcResult := model.Init
2928
crcResult = addBytes(data, model, crcResult, table)

checksum/crc8_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// crc8_test.go
2+
// description: Test for calculate crc8
3+
// author(s) [red_byte](https://github.com/i-redbyte)
4+
// see crc8.go
5+
6+
package checksum_test
7+
8+
import (
9+
"fmt"
10+
"testing"
11+
12+
"github.com/TheAlgorithms/Go/checksum"
13+
)
14+
15+
var (
16+
CRC8 = checksum.CRCModel{0x07, 0x00, false, false, 0x00, "CRC-8"}
17+
CRC8CDMA2000 = checksum.CRCModel{0x9B, 0xFF, false, false, 0x00, "CRC-8/CDMA2000"}
18+
CRC8DARC = checksum.CRCModel{0x39, 0x00, true, true, 0x00, "CRC-8/DARC"}
19+
CRC8DVBS2 = checksum.CRCModel{0xD5, 0x00, false, false, 0x00, "CRC-8/DVB-S2"}
20+
CRC8EBU = checksum.CRCModel{0x1D, 0xFF, true, true, 0x00, "CRC-8/EBU"}
21+
CRC8ICODE = checksum.CRCModel{0x1D, 0xFD, false, false, 0x00, "CRC-8/I-CODE"}
22+
CRC8ITU = checksum.CRCModel{0x07, 0x00, false, false, 0x55, "CRC-8/ITU"}
23+
CRC8MAXIM = checksum.CRCModel{0x31, 0x00, true, true, 0x00, "CRC-8/MAXIM"}
24+
CRC8ROHC = checksum.CRCModel{0x07, 0xFF, true, true, 0x00, "CRC-8/ROHC"}
25+
CRC8WCDMA = checksum.CRCModel{0x9B, 0x00, true, true, 0x00, "CRC-8/WCDMA"}
26+
)
27+
28+
func TestCalculateCRC8(t *testing.T) {
29+
data := []byte("123456789")
30+
tests := []struct {
31+
name string
32+
data []byte
33+
model checksum.CRCModel
34+
want uint8
35+
}{
36+
{CRC8.Name, data, CRC8, 0xF4},
37+
{CRC8CDMA2000.Name, data, CRC8CDMA2000, 0xDA},
38+
{CRC8DARC.Name, data, CRC8DARC, 0x15},
39+
{CRC8DVBS2.Name, data, CRC8DVBS2, 0xBC},
40+
{CRC8EBU.Name, data, CRC8EBU, 0x97},
41+
{CRC8ICODE.Name, data, CRC8ICODE, 0x7E},
42+
{CRC8ITU.Name, data, CRC8ITU, 0xA1},
43+
{CRC8MAXIM.Name, data, CRC8MAXIM, 0xA1},
44+
{CRC8ROHC.Name, data, CRC8ROHC, 0xD0},
45+
{CRC8WCDMA.Name, data, CRC8WCDMA, 0x25},
46+
}
47+
for _, test := range tests {
48+
t.Run(test.name, func(t *testing.T) {
49+
fmt.Println(checksum.CRC8(test.data, test.model))
50+
if got := checksum.CRC8(test.data, test.model); got != test.want {
51+
t.Errorf("CalculateCRC8() = %v, want %v", got, test.want)
52+
}
53+
})
54+
}
55+
}
56+
57+
func BenchmarkCalculateCRC8(b *testing.B) {
58+
for i := 0; i < b.N; i++ {
59+
checksum.CRC8([]byte("123456789"), CRC8)
60+
}
61+
}

checksum/luhn.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// Package checksum describes algorithms for finding various checksums
88
package checksum
99

10-
// LuhnAlgorithm This function calculates the checksum using the Luna algorithm
11-
func LuhnAlgorithm(s []rune) bool {
10+
// Luhn validates the provided data using the Luhn algorithm.
11+
func Luhn(s []byte) bool {
1212
n := len(s)
1313
number := 0
1414
result := 0

checksum/luhn_test.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,31 @@
33
// author(s) [red_byte](https://github.com/i-redbyte)
44
// see luhn.go
55

6-
package checksum
6+
package checksum_test
77

8-
import "testing"
8+
import (
9+
"testing"
910

10-
func TestLuhnAlgorithm(t *testing.T) {
11+
"github.com/TheAlgorithms/Go/checksum"
12+
)
13+
14+
func TestLuhn(t *testing.T) {
1115
tests := []struct {
1216
name string
13-
s []rune
17+
s []byte
1418
want bool
1519
}{
16-
{"check 4242424242424242", []rune("4242424242424242"), true},
17-
{"check 6200000000000005", []rune("6200000000000005"), true},
18-
{"check 5534200028533164", []rune("5534200028533164"), true},
19-
{"check 36227206271667", []rune("36227206271667"), true},
20-
{"check 471629309440", []rune("471629309440"), false},
21-
{"check 1111", []rune("1111"), false},
22-
{"check 12345674", []rune("12345674"), true},
20+
{"check 4242424242424242", []byte("4242424242424242"), true},
21+
{"check 6200000000000005", []byte("6200000000000005"), true},
22+
{"check 5534200028533164", []byte("5534200028533164"), true},
23+
{"check 36227206271667", []byte("36227206271667"), true},
24+
{"check 471629309440", []byte("471629309440"), false},
25+
{"check 1111", []byte("1111"), false},
26+
{"check 12345674", []byte("12345674"), true},
2327
}
2428
for _, test := range tests {
2529
t.Run(test.name, func(t *testing.T) {
26-
if got := LuhnAlgorithm(test.s); got != test.want {
30+
if got := checksum.Luhn(test.s); got != test.want {
2731
t.Errorf("LuhnAlgorithm() = %v, want %v", got, test.want)
2832
}
2933
})
@@ -32,6 +36,6 @@ func TestLuhnAlgorithm(t *testing.T) {
3236

3337
func BenchmarkBruteForceFactorial(b *testing.B) {
3438
for i := 0; i < b.N; i++ {
35-
LuhnAlgorithm([]rune("4242424242424242"))
39+
checksum.Luhn([]byte("4242424242424242"))
3640
}
3741
}

0 commit comments

Comments
 (0)