| 1 |
#include <pbcopper/cuda/AsciiConversion.h> |
| 2 |
|
| 3 |
#include <gtest/gtest.h> |
| 4 |
|
| 5 |
#include <pbcopper/container/DNAString.h> |
| 6 |
|
| 7 |
#include <array> |
| 8 |
#include <cstdint> |
| 9 |
#include <cstring> |
| 10 |
#include <random> |
| 11 |
#include <string> |
| 12 |
|
| 13 |
using namespace PacBio; |
| 14 |
|
| 15 |
TEST(Cuda_AsciiConversion, SingleBase) |
| 16 |
{ |
| 17 |
{ |
| 18 |
uint32_t arr[8] = {'A'}; |
| 19 |
const auto result = Cuda::AsciiToBitmaskContainer<64, 2>(arr, 1); |
| 20 |
EXPECT_EQ(result.RawData(), 0); |
| 21 |
} |
| 22 |
|
| 23 |
{ |
| 24 |
uint32_t arr[8] = {'C'}; |
| 25 |
const auto result = Cuda::AsciiToBitmaskContainer<64, 2>(arr, 1); |
| 26 |
EXPECT_EQ(result.RawData(), 1); |
| 27 |
} |
| 28 |
|
| 29 |
{ |
| 30 |
uint32_t arr[8] = {'G'}; |
| 31 |
const auto result = Cuda::AsciiToBitmaskContainer<64, 2>(arr, 1); |
| 32 |
EXPECT_EQ(result.RawData(), 2); |
| 33 |
} |
| 34 |
|
| 35 |
{ |
| 36 |
uint32_t arr[8] = {'T'}; |
| 37 |
const auto result = Cuda::AsciiToBitmaskContainer<64, 2>(arr, 1); |
| 38 |
EXPECT_EQ(result.RawData(), 3); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
TEST(Cuda_AsciiConversion, Comprehensive) |
| 43 |
{ |
| 44 |
std::random_device rd; |
| 45 |
std::mt19937 gen{rd()}; |
| 46 |
|
| 47 |
const int32_t rounds = 100'000; |
| 48 |
|
| 49 |
constexpr std::array<char, 4> DNA_BASES{'A', 'C', 'G', 'T'}; |
| 50 |
std::uniform_int_distribution<int32_t> baseDistribution{0, 3}; |
| 51 |
std::uniform_int_distribution<int32_t> lengthDistribution{0, 31}; |
| 52 |
|
| 53 |
for (int32_t r = 0; r < rounds; ++r) { |
| 54 |
// generate a random ASCII string |
| 55 |
const int32_t length = lengthDistribution(gen); |
| 56 |
std::string ascii1; |
| 57 |
for (int32_t i = 0; i < length; ++i) { |
| 58 |
ascii1.push_back(DNA_BASES[baseDistribution(gen)]); |
| 59 |
} |
| 60 |
|
| 61 |
uint32_t arr1[8] = {}; |
| 62 |
std::memcpy(arr1, ascii1.c_str(), length); |
| 63 |
|
| 64 |
// test CUDA ASCII → 2-bit conversion |
| 65 |
const auto comp1 = Cuda::AsciiToBitmaskContainer<64, 2>(arr1, length); |
| 66 |
const Container::DNA2bitStringVariable64 dna1{ascii1}; |
| 67 |
EXPECT_EQ(dna1.RawData(), comp1.RawData()); |
| 68 |
EXPECT_EQ(dna1.Length(), length); |
| 69 |
|
| 70 |
// test CUDA 2-bit conversion → ASCII |
| 71 |
uint32_t arr2[8] = {}; |
| 72 |
Cuda::BitmaskContainerToAscii(comp1, length, arr2); |
| 73 |
EXPECT_EQ(std::strcmp(ascii1.c_str(), reinterpret_cast<const char*>(arr2)), 0); |
| 74 |
static_assert(sizeof(arr1) == 32); |
| 75 |
EXPECT_EQ(std::memcmp(arr1, arr2, sizeof(arr1)), 0); |
| 76 |
|
| 77 |
// test CUDA ASCII → 2-bit conversion (roundtrip) |
| 78 |
const auto comp2 = Cuda::AsciiToBitmaskContainer<64, 2>(arr2, length); |
| 79 |
EXPECT_EQ(dna1.RawData(), comp2.RawData()); |
| 80 |
EXPECT_EQ(comp1, comp2); |
| 81 |
} |
| 82 |
} |