Implement IntArrayView.copyToBuffer().

Bug: 14425059
Change-Id: Ic1b7de72701eae0c0b1bb28d12514099446a66a9
This commit is contained in:
Keisuke Kuroyanagi 2014-09-11 15:21:52 +09:00
parent 4634b75d34
commit a3b0eb1685
2 changed files with 30 additions and 1 deletions

View File

@ -17,8 +17,9 @@
#ifndef LATINIME_INT_ARRAY_VIEW_H
#define LATINIME_INT_ARRAY_VIEW_H
#include <array>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <vector>
#include "defines.h"
@ -103,6 +104,12 @@ class IntArrayView {
return IntArrayView(mPtr + n, mSize - n);
}
template <size_t N>
void copyToArray(std::array<int, N> *const buffer, const size_t offset) const {
ASSERT(mSize + offset <= N);
memmove(buffer->data() + offset, mPtr, sizeof(int) * mSize);
}
private:
DISALLOW_ASSIGNMENT_OPERATOR(IntArrayView);

View File

@ -18,6 +18,7 @@
#include <gtest/gtest.h>
#include <array>
#include <vector>
namespace latinime {
@ -89,5 +90,26 @@ TEST(IntArrayViewTest, TestSkip) {
}
}
TEST(IntArrayViewTest, TestCopyToArray) {
// "{{" to suppress warning.
std::array<int, 7> buffer = {{10, 20, 30, 40, 50, 60, 70}};
const std::vector<int> intVector = {3, 2, 1, 0, -1, -2};
IntArrayView intArrayView(intVector);
intArrayView.limit(0).copyToArray(&buffer, 0);
EXPECT_EQ(10, buffer[0]);
EXPECT_EQ(20, buffer[1]);
intArrayView.limit(1).copyToArray(&buffer, 0);
EXPECT_EQ(intVector[0], buffer[0]);
EXPECT_EQ(20, buffer[1]);
intArrayView.limit(1).copyToArray(&buffer, 1);
EXPECT_EQ(intVector[0], buffer[0]);
EXPECT_EQ(intVector[0], buffer[1]);
intArrayView.copyToArray(&buffer, 0);
for (size_t i = 0; i < intArrayView.size(); ++i) {
EXPECT_EQ(intVector[i], buffer[i]);
}
EXPECT_EQ(70, buffer[6]);
}
} // namespace
} // namespace latinime