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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 const int64 kLongs[] = { 0, 1, -1, kint64min, kint64max}; | 79 const int64 kLongs[] = { 0, 1, -1, kint64min, kint64max}; |
80 const size_t kLen = arraysize(kLongs); | 80 const size_t kLen = arraysize(kLongs); |
81 | 81 |
82 JNIEnv* env = AttachCurrentThread(); | 82 JNIEnv* env = AttachCurrentThread(); |
83 CheckLongConversion(env, kLongs, kLen, ToJavaLongArray(env, kLongs, kLen)); | 83 CheckLongConversion(env, kLongs, kLen, ToJavaLongArray(env, kLongs, kLen)); |
84 | 84 |
85 const std::vector<int64> vec(kLongs, kLongs + kLen); | 85 const std::vector<int64> vec(kLongs, kLongs + kLen); |
86 CheckLongConversion(env, kLongs, kLen, ToJavaLongArray(env, vec)); | 86 CheckLongConversion(env, kLongs, kLen, ToJavaLongArray(env, vec)); |
87 } | 87 } |
88 | 88 |
| 89 void CheckFloatConversion( |
| 90 JNIEnv* env, |
| 91 const float* float_array, |
| 92 const size_t len, |
| 93 const ScopedJavaLocalRef<jfloatArray>& floats) { |
| 94 ASSERT_TRUE(floats.obj()); |
| 95 |
| 96 jsize java_array_len = env->GetArrayLength(floats.obj()); |
| 97 ASSERT_EQ(static_cast<jsize>(len), java_array_len); |
| 98 |
| 99 jfloat value; |
| 100 for (size_t i = 0; i < len; ++i) { |
| 101 env->GetFloatArrayRegion(floats.obj(), i, 1, &value); |
| 102 ASSERT_EQ(float_array[i], value); |
| 103 } |
| 104 } |
| 105 |
| 106 TEST(JniArray, FloatConversions) { |
| 107 const float kFloats[] = { 0.0f, 1.0f, -10.0f}; |
| 108 const size_t kLen = arraysize(kFloats); |
| 109 |
| 110 JNIEnv* env = AttachCurrentThread(); |
| 111 CheckFloatConversion(env, kFloats, kLen, |
| 112 ToFloatLongArray(env, kFloats, kLen)); |
| 113 } |
| 114 |
89 TEST(JniArray, JavaIntArrayToIntVector) { | 115 TEST(JniArray, JavaIntArrayToIntVector) { |
90 const int kInts[] = {0, 1, -1}; | 116 const int kInts[] = {0, 1, -1}; |
91 const size_t kLen = arraysize(kInts); | 117 const size_t kLen = arraysize(kInts); |
92 | 118 |
93 JNIEnv* env = AttachCurrentThread(); | 119 JNIEnv* env = AttachCurrentThread(); |
94 ScopedJavaLocalRef<jintArray> jints(env, env->NewIntArray(kLen)); | 120 ScopedJavaLocalRef<jintArray> jints(env, env->NewIntArray(kLen)); |
95 ASSERT_TRUE(jints.obj()); | 121 ASSERT_TRUE(jints.obj()); |
96 | 122 |
97 for (size_t i = 0; i < kLen; ++i) { | 123 for (size_t i = 0; i < kLen; ++i) { |
98 jint j = static_cast<jint>(kInts[i]); | 124 jint j = static_cast<jint>(kInts[i]); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 196 |
171 EXPECT_EQ(static_cast<size_t>(kMaxItems), vec.size()); | 197 EXPECT_EQ(static_cast<size_t>(kMaxItems), vec.size()); |
172 for (int i = 0; i < kMaxItems; ++i) { | 198 for (int i = 0; i < kMaxItems; ++i) { |
173 snprintf(text, sizeof text, "%d", i); | 199 snprintf(text, sizeof text, "%d", i); |
174 EXPECT_STREQ(text, vec[i].c_str()); | 200 EXPECT_STREQ(text, vec[i].c_str()); |
175 } | 201 } |
176 } | 202 } |
177 | 203 |
178 } // namespace android | 204 } // namespace android |
179 } // namespace base | 205 } // namespace base |
OLD | NEW |