OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/android/jni_array.h" | 5 #include "base/android/jni_array.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 105 |
106 ASSERT_EQ(static_cast<jsize>(ints.size()), env->GetArrayLength(jints.obj())); | 106 ASSERT_EQ(static_cast<jsize>(ints.size()), env->GetArrayLength(jints.obj())); |
107 | 107 |
108 jint value; | 108 jint value; |
109 for (size_t i = 0; i < kLen; ++i) { | 109 for (size_t i = 0; i < kLen; ++i) { |
110 env->GetIntArrayRegion(jints.obj(), i, 1, &value); | 110 env->GetIntArrayRegion(jints.obj(), i, 1, &value); |
111 ASSERT_EQ(ints[i], value); | 111 ASSERT_EQ(ints[i], value); |
112 } | 112 } |
113 } | 113 } |
114 | 114 |
| 115 TEST(JniArray, JavaLongArrayToInt64Vector) { |
| 116 const int64 kInt64s[] = {0LL, 1LL, -1LL}; |
| 117 const size_t kLen = arraysize(kInt64s); |
| 118 |
| 119 JNIEnv* env = AttachCurrentThread(); |
| 120 ScopedJavaLocalRef<jlongArray> jlongs(env, env->NewLongArray(kLen)); |
| 121 ASSERT_TRUE(jlongs.obj()); |
| 122 |
| 123 for (size_t i = 0; i < kLen; ++i) { |
| 124 jlong j = static_cast<jlong>(kInt64s[i]); |
| 125 env->SetLongArrayRegion(jlongs.obj(), i, 1, &j); |
| 126 ASSERT_FALSE(HasException(env)); |
| 127 } |
| 128 |
| 129 std::vector<int64> int64s; |
| 130 JavaLongArrayToInt64Vector(env, jlongs.obj(), &int64s); |
| 131 |
| 132 ASSERT_EQ(static_cast<jsize>(int64s.size()), |
| 133 env->GetArrayLength(jlongs.obj())); |
| 134 |
| 135 jlong value; |
| 136 for (size_t i = 0; i < kLen; ++i) { |
| 137 env->GetLongArrayRegion(jlongs.obj(), i, 1, &value); |
| 138 ASSERT_EQ(int64s[i], value); |
| 139 ASSERT_EQ(kInt64s[i], int64s[i]); |
| 140 } |
| 141 } |
| 142 |
| 143 TEST(JniArray, JavaLongArrayToLongVector) { |
| 144 const int64 kInt64s[] = {0LL, 1LL, -1LL}; |
| 145 const size_t kLen = arraysize(kInt64s); |
| 146 |
| 147 JNIEnv* env = AttachCurrentThread(); |
| 148 ScopedJavaLocalRef<jlongArray> jlongs(env, env->NewLongArray(kLen)); |
| 149 ASSERT_TRUE(jlongs.obj()); |
| 150 |
| 151 for (size_t i = 0; i < kLen; ++i) { |
| 152 jlong j = static_cast<jlong>(kInt64s[i]); |
| 153 env->SetLongArrayRegion(jlongs.obj(), i, 1, &j); |
| 154 ASSERT_FALSE(HasException(env)); |
| 155 } |
| 156 |
| 157 std::vector<jlong> jlongs_vector; |
| 158 JavaLongArrayToLongVector(env, jlongs.obj(), &jlongs_vector); |
| 159 |
| 160 ASSERT_EQ(static_cast<jsize>(jlongs_vector.size()), |
| 161 env->GetArrayLength(jlongs.obj())); |
| 162 |
| 163 jlong value; |
| 164 for (size_t i = 0; i < kLen; ++i) { |
| 165 env->GetLongArrayRegion(jlongs.obj(), i, 1, &value); |
| 166 ASSERT_EQ(jlongs_vector[i], value); |
| 167 } |
| 168 } |
| 169 |
115 TEST(JniArray, JavaFloatArrayToFloatVector) { | 170 TEST(JniArray, JavaFloatArrayToFloatVector) { |
116 const float kFloats[] = {0.0, 0.5, -0.5}; | 171 const float kFloats[] = {0.0, 0.5, -0.5}; |
117 const size_t kLen = arraysize(kFloats); | 172 const size_t kLen = arraysize(kFloats); |
118 | 173 |
119 JNIEnv* env = AttachCurrentThread(); | 174 JNIEnv* env = AttachCurrentThread(); |
120 ScopedJavaLocalRef<jfloatArray> jfloats(env, env->NewFloatArray(kLen)); | 175 ScopedJavaLocalRef<jfloatArray> jfloats(env, env->NewFloatArray(kLen)); |
121 ASSERT_TRUE(jfloats.obj()); | 176 ASSERT_TRUE(jfloats.obj()); |
122 | 177 |
123 for (size_t i = 0; i < kLen; ++i) { | 178 for (size_t i = 0; i < kLen; ++i) { |
124 jfloat j = static_cast<jfloat>(kFloats[i]); | 179 jfloat j = static_cast<jfloat>(kFloats[i]); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 225 |
171 EXPECT_EQ(static_cast<size_t>(kMaxItems), vec.size()); | 226 EXPECT_EQ(static_cast<size_t>(kMaxItems), vec.size()); |
172 for (int i = 0; i < kMaxItems; ++i) { | 227 for (int i = 0; i < kMaxItems; ++i) { |
173 snprintf(text, sizeof text, "%d", i); | 228 snprintf(text, sizeof text, "%d", i); |
174 EXPECT_STREQ(text, vec[i].c_str()); | 229 EXPECT_STREQ(text, vec[i].c_str()); |
175 } | 230 } |
176 } | 231 } |
177 | 232 |
178 } // namespace android | 233 } // namespace android |
179 } // namespace base | 234 } // namespace base |
OLD | NEW |