forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
123e464
commit 656920c
Showing
4 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
#include "velox/common/encode/Base32.h" | ||
#include <cstring> | ||
|
||
namespace facebook::velox::encoding{ | ||
|
||
constexpr Base32::ReverseIndex kBase32ReverseIndexTable = { | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | ||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | ||
25, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255}; | ||
|
||
// Test cases for Base32::calculateDecodedSize | ||
TEST(Base32Test, CalculateDecodedSizeEmptyInput) { | ||
std::string_view input = ""; | ||
size_t inputSize = 0; | ||
size_t decodedSize = 0; | ||
|
||
auto status = Base32::calculateDecodedSize(input, inputSize, decodedSize); | ||
EXPECT_TRUE(status.ok()); | ||
EXPECT_EQ(decodedSize, 0); | ||
} | ||
|
||
TEST(Base32Test, CalculateDecodedSizePaddedInput) { | ||
std::string_view input = "MY======"; // Base32 encoded "f" | ||
size_t inputSize = input.size(); | ||
size_t decodedSize = 0; | ||
|
||
auto status = Base32::calculateDecodedSize(input, inputSize, decodedSize); | ||
EXPECT_TRUE(status.ok()); | ||
EXPECT_EQ(decodedSize, 1); // "f" is 1 byte | ||
} | ||
|
||
TEST(Base32Test, CalculateDecodedSizeUnpaddedInput) { | ||
std::string_view input = "MZXW6YTBOI======"; | ||
size_t inputSize = input.size(); | ||
size_t decodedSize = 0; | ||
|
||
auto status = Base32::calculateDecodedSize(input, inputSize, decodedSize); | ||
EXPECT_TRUE(status.ok()); | ||
EXPECT_EQ(decodedSize, 6); | ||
} | ||
|
||
// Test cases for Base32::base32ReverseLookup | ||
TEST(Base32Test, Base32ReverseLookupValidChar) { | ||
Status status; | ||
uint8_t result = Base32::base32ReverseLookup('M', kBase32ReverseIndexTable, status); | ||
EXPECT_TRUE(status.ok()); | ||
EXPECT_EQ(result, 12); | ||
} | ||
|
||
TEST(Base32Test, Base32ReverseLookupInvalidChar) { | ||
Status status; | ||
uint8_t result = Base32::base32ReverseLookup('@', kBase32ReverseIndexTable, status); // '@' is not in Base32 charset | ||
EXPECT_FALSE(status.ok()); | ||
EXPECT_EQ(result, 0); | ||
} | ||
|
||
// Test cases for Base32::decodeImpl | ||
TEST(Base32Test, DecodeImplValidInput) { | ||
std::string_view input = "MZXW6YTBOI======"; | ||
size_t inputSize = input.size(); | ||
char output[6] = {0}; | ||
size_t outputSize = sizeof(output); | ||
|
||
auto status = Base32::decodeImpl(input, inputSize, output, outputSize, kBase32ReverseIndexTable); | ||
EXPECT_TRUE(status.ok()); | ||
EXPECT_STREQ(output, "foobar"); | ||
} | ||
|
||
TEST(Base32Test, DecodeImplInvalidInputLength) { | ||
std::string_view input = "MZXW6"; | ||
size_t inputSize = 3; | ||
char output[5]; | ||
size_t outputSize = sizeof(output); | ||
|
||
auto status = Base32::decodeImpl(input, inputSize, output, outputSize, kBase32ReverseIndexTable); | ||
EXPECT_FALSE(status.ok()); | ||
} | ||
|
||
TEST(Base32Test, DecodeImplOutputBufferTooSmall) { | ||
std::string_view input = "MZXW6YQ="; // Base32 encoded "foobar" | ||
size_t inputSize = input.size(); | ||
char output[3]; // Too small for decoded output | ||
size_t outputSize = sizeof(output); | ||
|
||
auto status = Base32::decodeImpl(input, inputSize, output, outputSize, kBase32ReverseIndexTable); | ||
EXPECT_FALSE(status.ok()); | ||
EXPECT_EQ(status.message(), "Base32::decode() - output buffer too small."); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters