Skip to content

Commit

Permalink
Fix failing unit tests on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqr authored and tgoyne committed Sep 23, 2019
1 parent dd59fc9 commit 1122c08
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 58 deletions.
128 changes: 70 additions & 58 deletions tests/tests/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ TEST(lagi_audio, save_audio_clip) {
agi::SaveAudioClip(*provider, path, 60 * 60 * 1000, (60 * 60 + 10) * 1000);

{
bfs::ifstream s(path);
bfs::ifstream s(path, std::ios_base::binary);
ASSERT_TRUE(s.good());
s.seekg(0, std::ios::end);
// 10 seconds of 44.1 kHz samples per second of 16-bit mono, plus 44 bytes of header
Expand All @@ -141,7 +141,7 @@ TEST(lagi_audio, save_audio_clip_out_of_audio_range) {
// Start time after end of clip: empty file
agi::SaveAudioClip(*provider, path, end_time, end_time + 1);
{
bfs::ifstream s(path);
bfs::ifstream s(path, std::ios_base::binary);
ASSERT_TRUE(s.good());
s.seekg(0, std::ios::end);
EXPECT_EQ(44, s.tellg());
Expand All @@ -151,7 +151,7 @@ TEST(lagi_audio, save_audio_clip_out_of_audio_range) {
// Start time >= end time: empty file
agi::SaveAudioClip(*provider, path, end_time - 1, end_time - 1);
{
bfs::ifstream s(path);
bfs::ifstream s(path, std::ios_base::binary);
ASSERT_TRUE(s.good());
s.seekg(0, std::ios::end);
EXPECT_EQ(44, s.tellg());
Expand All @@ -161,7 +161,7 @@ TEST(lagi_audio, save_audio_clip_out_of_audio_range) {
// Start time during clip, end time after end of clip: save only the part that exists
agi::SaveAudioClip(*provider, path, end_time - 1000, end_time + 1000);
{
bfs::ifstream s(path);
bfs::ifstream s(path, std::ios_base::binary);
ASSERT_TRUE(s.good());
s.seekg(0, std::ios::end);
// 1 second of 44.1 kHz samples per second of 16-bit mono, plus 44 bytes of header
Expand Down Expand Up @@ -365,18 +365,20 @@ TEST(lagi_audio, pcm_simple) {
agi::SaveAudioClip(provider, path, 0, 1000);
}

auto provider = agi::CreatePCMAudioProvider(path, nullptr);
EXPECT_EQ(1, provider->GetChannels());
EXPECT_EQ(48000, provider->GetNumSamples());
EXPECT_EQ(48000, provider->GetSampleRate());
EXPECT_EQ(2, provider->GetBytesPerSample());
EXPECT_EQ(false, provider->AreSamplesFloat());
EXPECT_EQ(false, provider->NeedsCache());

for (int i = 0; i < 100; ++i) {
uint16_t sample;
provider->GetAudio(&sample, i, 1);
ASSERT_EQ(i, sample);
{
auto provider = agi::CreatePCMAudioProvider(path, nullptr);
EXPECT_EQ(1, provider->GetChannels());
EXPECT_EQ(48000, provider->GetNumSamples());
EXPECT_EQ(48000, provider->GetSampleRate());
EXPECT_EQ(2, provider->GetBytesPerSample());
EXPECT_EQ(false, provider->AreSamplesFloat());
EXPECT_EQ(false, provider->NeedsCache());

for (int i = 0; i < 100; ++i) {
uint16_t sample;
provider->GetAudio(&sample, i, 1);
ASSERT_EQ(i, sample);
}
}

agi::fs::Remove(path);
Expand All @@ -391,39 +393,41 @@ TEST(lagi_audio, pcm_truncated) {

char file[1000];

{ bfs::ifstream s(path); s.read(file, sizeof file); }
{ bfs::ofstream s(path); s.write(file, sizeof file); }
{ bfs::ifstream s(path, std::ios_base::binary); s.read(file, sizeof file); }
{ bfs::ofstream s(path, std::ios_base::binary); s.write(file, sizeof file); }

auto provider = agi::CreatePCMAudioProvider(path, nullptr);
{
auto provider = agi::CreatePCMAudioProvider(path, nullptr);

// Should still report full duration
EXPECT_EQ(48000, provider->GetNumSamples());
// Should still report full duration
EXPECT_EQ(48000, provider->GetNumSamples());

// And should zero-pad past the end
auto sample_count = (1000 - 44) / 2;
uint16_t sample;
// And should zero-pad past the end
int64_t sample_count = (1000 - 44) / 2;
uint16_t sample;

provider->GetAudio(&sample, sample_count - 1, 1);
EXPECT_EQ(sample_count - 1, sample);
provider->GetAudio(&sample, sample_count - 1, 1);
EXPECT_EQ(sample_count - 1, sample);

provider->GetAudio(&sample, sample_count, 1);
EXPECT_EQ(0, sample);
provider->GetAudio(&sample, sample_count, 1);
EXPECT_EQ(0, sample);
}

agi::fs::Remove(path);
}

#define RIFF "RIFF\0\0\0\x60WAVE"
#define FMT_VALID "fmt \20\0\0\0\1\0\1\0\20\0\0\0\0\0\0\0\0\0\20\0"
#define DATA_VALID "data\1\0\0\0\0\0"
#define WRITE(str) do { bfs::ofstream s(path); s.write(str, sizeof(str) - 1); } while (false)
#define WRITE(str) do { bfs::ofstream s(path, std::ios_base::binary); s.write(str, sizeof(str) - 1); } while (false)

TEST(lagi_audio, pcm_incomplete) {
auto path = agi::Path().Decode("?temp/pcm_incomplete");

agi::fs::Remove(path);
ASSERT_THROW(agi::CreatePCMAudioProvider(path, nullptr), agi::fs::FileNotFound);

bfs::ofstream{path};
{bfs::ofstream(path, std::ios_base::binary); }
ASSERT_THROW(agi::CreatePCMAudioProvider(path, nullptr), agi::AudioDataNotFound);

// Invalid tags
Expand All @@ -438,8 +442,10 @@ TEST(lagi_audio, pcm_incomplete) {

// -1 for nul term, -3 so that longest file is still invalid
for (size_t i = 0; i < sizeof(valid_file) - 4; ++i) {
bfs::ofstream s(path);
s.write(valid_file, i);
{
bfs::ofstream s(path, std::ios_base::binary);
s.write(valid_file, i);
}
ASSERT_THROW(agi::CreatePCMAudioProvider(path, nullptr), agi::AudioDataNotFound);
}

Expand All @@ -463,28 +469,30 @@ TEST(lagi_audio, multiple_data_chunks) {

WRITE(RIFF FMT_VALID "data\2\0\0\0\1\0" "data\2\0\0\0\2\0" "data\2\0\0\0\3\0");

auto provider = agi::CreatePCMAudioProvider(path, nullptr);
ASSERT_EQ(3, provider->GetNumSamples());
{
auto provider = agi::CreatePCMAudioProvider(path, nullptr);
ASSERT_EQ(3, provider->GetNumSamples());

uint16_t samples[3];
uint16_t samples[3];

provider->GetAudio(samples, 0, 3);
EXPECT_EQ(1, samples[0]);
EXPECT_EQ(2, samples[1]);
EXPECT_EQ(3, samples[2]);
provider->GetAudio(samples, 0, 3);
EXPECT_EQ(1, samples[0]);
EXPECT_EQ(2, samples[1]);
EXPECT_EQ(3, samples[2]);

samples[1] = 5;
provider->GetAudio(samples, 2, 1);
EXPECT_EQ(3, samples[0]);
EXPECT_EQ(5, samples[1]);
samples[1] = 5;
provider->GetAudio(samples, 2, 1);
EXPECT_EQ(3, samples[0]);
EXPECT_EQ(5, samples[1]);

provider->GetAudio(samples, 1, 1);
EXPECT_EQ(2, samples[0]);
EXPECT_EQ(5, samples[1]);
provider->GetAudio(samples, 1, 1);
EXPECT_EQ(2, samples[0]);
EXPECT_EQ(5, samples[1]);

provider->GetAudio(samples, 0, 1);
EXPECT_EQ(1, samples[0]);
EXPECT_EQ(5, samples[1]);
provider->GetAudio(samples, 0, 1);
EXPECT_EQ(1, samples[0]);
EXPECT_EQ(5, samples[1]);
}

agi::fs::Remove(path);
}
Expand All @@ -504,13 +512,15 @@ TEST(lagi_audio, wave64_simple) {
auto path = agi::Path().Decode("?temp/w64_valid");
WRITE(WAVE64_FILE);

auto provider = agi::CreatePCMAudioProvider(path, nullptr);
ASSERT_EQ(2, provider->GetNumSamples());
{
auto provider = agi::CreatePCMAudioProvider(path, nullptr);
ASSERT_EQ(2, provider->GetNumSamples());

uint16_t samples[2];
provider->GetAudio(samples, 0, 2);
EXPECT_EQ(1, samples[0]);
EXPECT_EQ(2, samples[1]);
uint16_t samples[2];
provider->GetAudio(samples, 0, 2);
EXPECT_EQ(1, samples[0]);
EXPECT_EQ(2, samples[1]);
}

agi::fs::Remove(path);
}
Expand All @@ -520,13 +530,15 @@ TEST(lagi_audio, wave64_truncated) {

// Should be invalid until there's an entire sample
for (size_t i = 0; i < sizeof(WAVE64_FILE) - 4; ++i) {
bfs::ofstream s(path);
s.write(WAVE64_FILE, i);
{
bfs::ofstream s(path, std::ios_base::binary);
s.write(WAVE64_FILE, i);
}
ASSERT_THROW(agi::CreatePCMAudioProvider(path, nullptr), agi::AudioDataNotFound);
}

{
bfs::ofstream s(path);
bfs::ofstream s(path, std::ios_base::binary);
s.write(WAVE64_FILE, sizeof(WAVE64_FILE) - 3);
}
ASSERT_NO_THROW(agi::CreatePCMAudioProvider(path, nullptr));
Expand Down
9 changes: 9 additions & 0 deletions tests/tests/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,18 @@ TEST(lagi_path, make_relative_on_dummy_url) {
EXPECT_UNCHANGED("?dummy:23.976000:40000:1280:720:47:163:254:", MakeRelative);
}

#ifdef _WIN32
TEST(lagi_path, encode) {
Path p;
p.SetToken("?user", "C:\\a\\b\\c");
p.SetToken("?local", "C:\\a\\b\\c\\d");
EXPECT_EQ("?local\\e", p.Encode("C:\\a\\b\\c\\d\\e"));
}
#else
TEST(lagi_path, encode) {
Path p;
p.SetToken("?user", "/a/b/c");
p.SetToken("?local", "/a/b/c/d");
EXPECT_EQ("?local/e", p.Encode("/a/b/c/d/e"));
}
#endif

0 comments on commit 1122c08

Please sign in to comment.