1 /*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <fcntl.h>
18 #include <gtest/gtest.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <fstream>
24 #include <iostream>
25
26 #include "aptXbtenc.h"
27
28 #define BYTES_PER_CODEWORD 16
29
30 class LibAptxEncTest : public ::testing::Test {
31 private:
32 void* aptxbtenc = nullptr;
33
34 protected:
SetUp()35 void SetUp() override {
36 aptxbtenc = malloc(SizeofAptxbtenc());
37 ASSERT_NE(aptxbtenc, nullptr);
38 ASSERT_EQ(aptxbtenc_init(aptxbtenc, 0), 0);
39 }
40
TearDown()41 void TearDown() override { free(aptxbtenc); }
42
codeword_cmp(const uint16_t pcm[8],const uint32_t codeword)43 void codeword_cmp(const uint16_t pcm[8], const uint32_t codeword) {
44 uint32_t pcmL[4];
45 uint32_t pcmR[4];
46 for (size_t i = 0; i < 4; i++) {
47 pcmL[i] = pcm[0];
48 pcmR[i] = pcm[1];
49 pcm += 2;
50 }
51 uint32_t encoded_sample;
52 aptxbtenc_encodestereo(aptxbtenc, &pcmL, &pcmR, &encoded_sample);
53 ASSERT_EQ(encoded_sample, codeword);
54 }
55 };
56
TEST_F(LibAptxEncTest,encode_fake_data)57 TEST_F(LibAptxEncTest, encode_fake_data) {
58 const char input[] =
59 "012345678901234567890123456789012345678901234567890123456789012345678901"
60 "23456789";
61 const uint32_t aptx_codeword[] = {1270827967, 134154239, 670640127,
62 1280265295, 2485752873};
63
64 ASSERT_EQ((sizeof(input) - 1) % BYTES_PER_CODEWORD, 0);
65 ASSERT_EQ((sizeof(input) - 1) / BYTES_PER_CODEWORD,
66 sizeof(aptx_codeword) / sizeof(uint32_t));
67
68 size_t idx = 0;
69
70 uint16_t pcm[8];
71
72 while (idx * BYTES_PER_CODEWORD < sizeof(input) - 1) {
73 memcpy(pcm, input + idx * BYTES_PER_CODEWORD, BYTES_PER_CODEWORD);
74 codeword_cmp(pcm, aptx_codeword[idx]);
75 ++idx;
76 }
77 }
78