Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: base/android/jni_array_unittest.cc

Issue 1689563002: android: Add ToJavaFloatArray JNI utility. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added std::vector version. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/android/jni_array.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 ScopedJavaLocalRef<jintArray> jints, 106 ScopedJavaLocalRef<jintArray> jints,
107 std::vector<int> int_vector, 107 std::vector<int> int_vector,
108 const size_t len) { 108 const size_t len) {
109 jint value; 109 jint value;
110 for (size_t i = 0; i < len; ++i) { 110 for (size_t i = 0; i < len; ++i) {
111 env->GetIntArrayRegion(jints.obj(), i, 1, &value); 111 env->GetIntArrayRegion(jints.obj(), i, 1, &value);
112 ASSERT_EQ(int_vector[i], value); 112 ASSERT_EQ(int_vector[i], value);
113 } 113 }
114 } 114 }
115 115
116 void CheckFloatConversion(
117 JNIEnv* env,
118 const float* float_array,
119 const size_t len,
120 const ScopedJavaLocalRef<jfloatArray>& floats) {
121 ASSERT_TRUE(floats.obj());
122
123 jsize java_array_len = env->GetArrayLength(floats.obj());
124 ASSERT_EQ(static_cast<jsize>(len), java_array_len);
125
126 jfloat value;
127 for (size_t i = 0; i < len; ++i) {
128 env->GetFloatArrayRegion(floats.obj(), i, 1, &value);
129 ASSERT_EQ(float_array[i], value);
130 }
131 }
132
133 TEST(JniArray, FloatConversions) {
134 const float kFloats[] = { 0.0f, 1.0f, -10.0f};
135 const size_t kLen = arraysize(kFloats);
136
137 JNIEnv* env = AttachCurrentThread();
138 CheckFloatConversion(env, kFloats, kLen,
139 ToJavaFloatArray(env, kFloats, kLen));
140
141 const std::vector<float> vec(kFloats, kFloats + kLen);
142 CheckFloatConversion(env, kFloats, kLen, ToJavaFloatArray(env, vec));
143 }
144
116 TEST(JniArray, JavaIntArrayToIntVector) { 145 TEST(JniArray, JavaIntArrayToIntVector) {
117 const int kInts[] = {0, 1, -1}; 146 const int kInts[] = {0, 1, -1};
118 const size_t kLen = arraysize(kInts); 147 const size_t kLen = arraysize(kInts);
119 148
120 JNIEnv* env = AttachCurrentThread(); 149 JNIEnv* env = AttachCurrentThread();
121 ScopedJavaLocalRef<jintArray> jints(env, env->NewIntArray(kLen)); 150 ScopedJavaLocalRef<jintArray> jints(env, env->NewIntArray(kLen));
122 ASSERT_TRUE(jints.obj()); 151 ASSERT_TRUE(jints.obj());
123 152
124 for (size_t i = 0; i < kLen; ++i) { 153 for (size_t i = 0; i < kLen; ++i) {
125 jint j = static_cast<jint>(kInts[i]); 154 jint j = static_cast<jint>(kInts[i]);
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 322
294 EXPECT_EQ(kNumItems, out.size()); 323 EXPECT_EQ(kNumItems, out.size());
295 CheckIntArrayConversion(env, int_array0, out[0], kLen0); 324 CheckIntArrayConversion(env, int_array0, out[0], kLen0);
296 CheckIntArrayConversion(env, int_array1, out[1], kLen1); 325 CheckIntArrayConversion(env, int_array1, out[1], kLen1);
297 CheckIntArrayConversion(env, int_array2, out[2], kLen2); 326 CheckIntArrayConversion(env, int_array2, out[2], kLen2);
298 CheckIntArrayConversion(env, int_array3, out[3], kLen3); 327 CheckIntArrayConversion(env, int_array3, out[3], kLen3);
299 } 328 }
300 329
301 } // namespace android 330 } // namespace android
302 } // namespace base 331 } // namespace base
OLDNEW
« no previous file with comments | « base/android/jni_array.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698