From f4a739d6d630cd4a43529a4b470d4be9d336ef36 Mon Sep 17 00:00:00 2001 From: ashrr108 <70876192+ashrr108@users.noreply.github.com> Date: Tue, 6 Jun 2023 12:19:58 +0000 Subject: [PATCH] Test case generated by Roost.AI --- .../mainMaind247df73bb_test.go | 75 ++++++++++++++++++ .../mainMainb7d8114fa9_test.go | 79 +++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 18-base64-encoding-decoding/01-base64-encoding/mainMaind247df73bb_test.go create mode 100644 18-base64-encoding-decoding/02-base64-decoding/mainMainb7d8114fa9_test.go diff --git a/18-base64-encoding-decoding/01-base64-encoding/mainMaind247df73bb_test.go b/18-base64-encoding-decoding/01-base64-encoding/mainMaind247df73bb_test.go new file mode 100644 index 0000000..d351fc8 --- /dev/null +++ b/18-base64-encoding-decoding/01-base64-encoding/mainMaind247df73bb_test.go @@ -0,0 +1,75 @@ +package main + +import ( + "encoding/base64" + "fmt" + "testing" +) + +func encodeData(data string) (string, string, string, string) { + // Standard Base64 Encoding + encodedData := base64.StdEncoding.EncodeToString([]byte(data)) + + // Standard Base64 Encoding without padding + encodedDataWithoutPadding := base64.RawStdEncoding.EncodeToString([]byte(data)) + + // URL and filename-safe Base64 encoding + urlSafeEncodedData := base64.URLEncoding.EncodeToString([]byte(data)) + + // URL and filename-safe Base64 encoding without padding + urlSafeEncodedDataWithoutPadding := base64.RawURLEncoding.EncodeToString([]byte(data)) + + return encodedData, encodedDataWithoutPadding, urlSafeEncodedData, urlSafeEncodedDataWithoutPadding +} + +func TestEncodeData(t *testing.T) { + data := "Gol@ng is Awesome?~" + expectedEncodedData := "R29sQG5nIGlzIEF3ZXNvbWU/fg==" + expectedEncodedDataWithoutPadding := "R29sQG5nIGlzIEF3ZXNvbWU_fg" + expectedUrlSafeEncodedData := "R29sQG5nIGlzIEF3ZXNvbWU_fg==" + expectedUrlSafeEncodedDataWithoutPadding := "R29sQG5nIGlzIEF3ZXNvbWU_fg" + + encodedData, encodedDataWithoutPadding, urlSafeEncodedData, urlSafeEncodedDataWithoutPadding := encodeData(data) + + if encodedData != expectedEncodedData { + t.Errorf("Expected %s, but got %s", expectedEncodedData, encodedData) + } + + if encodedDataWithoutPadding != expectedEncodedDataWithoutPadding { + t.Errorf("Expected %s, but got %s", expectedEncodedDataWithoutPadding, encodedDataWithoutPadding) + } + + if urlSafeEncodedData != expectedUrlSafeEncodedData { + t.Errorf("Expected %s, but got %s", expectedUrlSafeEncodedData, urlSafeEncodedData) + } + + if urlSafeEncodedDataWithoutPadding != expectedUrlSafeEncodedDataWithoutPadding { + t.Errorf("Expected %s, but got %s", expectedUrlSafeEncodedDataWithoutPadding, urlSafeEncodedDataWithoutPadding) + } +} + +func TestEncodeDataWithEmptyString(t *testing.T) { + data := "" + expectedEncodedData := "" + expectedEncodedDataWithoutPadding := "" + expectedUrlSafeEncodedData := "" + expectedUrlSafeEncodedDataWithoutPadding := "" + + encodedData, encodedDataWithoutPadding, urlSafeEncodedData, urlSafeEncodedDataWithoutPadding := encodeData(data) + + if encodedData != expectedEncodedData { + t.Errorf("Expected empty string, but got %s", encodedData) + } + + if encodedDataWithoutPadding != expectedEncodedDataWithoutPadding { + t.Errorf("Expected empty string, but got %s", encodedDataWithoutPadding) + } + + if urlSafeEncodedData != expectedUrlSafeEncodedData { + t.Errorf("Expected empty string, but got %s", urlSafeEncodedData) + } + + if urlSafeEncodedDataWithoutPadding != expectedUrlSafeEncodedDataWithoutPadding { + t.Errorf("Expected empty string, but got %s", urlSafeEncodedDataWithoutPadding) + } +} \ No newline at end of file diff --git a/18-base64-encoding-decoding/02-base64-decoding/mainMainb7d8114fa9_test.go b/18-base64-encoding-decoding/02-base64-decoding/mainMainb7d8114fa9_test.go new file mode 100644 index 0000000..39d5225 --- /dev/null +++ b/18-base64-encoding-decoding/02-base64-decoding/mainMainb7d8114fa9_test.go @@ -0,0 +1,79 @@ +package main + +import ( + "encoding/base64" + "fmt" + "testing" +) + +func decodeBase64(encodedData string, urlSafe bool) (string, error) { + var decodedData []byte + var err error + + if urlSafe { + decodedData, err = base64.URLEncoding.DecodeString(encodedData) + } else { + decodedData, err = base64.StdEncoding.DecodeString(encodedData) + } + + if err != nil { + return "", err + } + return string(decodedData), nil +} + +func TestDecodeBase64(t *testing.T) { + // Test case 1: Standard Base64 decoding + encodedData := "R29sQG5nIGlzIEF3ZXNvbWU/fg==" + expectedResult := "Gol@ng is Awesome?~" + result, err := decodeBase64(encodedData, false) + if err != nil { + t.Errorf("Error decoding Base64 encoded data %v", err) + } + if result != expectedResult { + t.Errorf("Expected %s, got %s", expectedResult, result) + } + + // Test case 2: URL and filename-safe Base64 decoding + urlSafeBase64EncodedData := "R29sQG5nIGlzIEF3ZXNvbWU_fg==" + expectedUrlSafeResult := "Gol@ng is Awesome_~" + urlSafeResult, err := decodeBase64(urlSafeBase64EncodedData, true) + if err != nil { + t.Errorf("Error decoding Base64 encoded data %v", err) + } + if urlSafeResult != expectedUrlSafeResult { + t.Errorf("Expected %s, got %s", expectedUrlSafeResult, urlSafeResult) + } +} + +func TestDecodeBase64InvalidInput(t *testing.T) { + // Test case 3: Invalid input for Standard Base64 decoding + invalidEncodedData := "R29sQG5nIGlzIEF3ZXNvbWU?@" + _, err := decodeBase64(invalidEncodedData, false) + if err == nil { + t.Error("Expected an error, but got nil") + } + + // Test case 4: Invalid input for URL and filename-safe Base64 decoding + invalidUrlSafeBase64EncodedData := "R29sQG5nIGlzIEF3ZXNvbWU?@" + _, err = decodeBase64(invalidUrlSafeBase64EncodedData, true) + if err == nil { + t.Error("Expected an error, but got nil") + } +} + +func main() { + encodedData := "R29sQG5nIGlzIEF3ZXNvbWU/fg==" + decodedData, err := decodeBase64(encodedData, false) + if err != nil { + fmt.Printf("Error decoding Base64 encoded data %v", err) + } + fmt.Println(decodedData) + + urlSafeBase64EncodedData := "R29sQG5nIGlzIEF3ZXNvbWU_fg==" + urlSafeData, err := decodeBase64(urlSafeBase64EncodedData, true) + if err != nil { + fmt.Printf("Error decoding Base64 encoded data %v", err) + } + fmt.Println(urlSafeData) +} \ No newline at end of file