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

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

Issue 2877703006: Add functions to convert boolean jni array. (Closed)
Patch Set: Created 3 years, 7 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') | chrome/browser/android/chrome_backup_agent.cc » ('j') | 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 27 matching lines...) Expand all
38 std::vector<uint8_t> expected_vec(kBytes, kBytes + kLen); 38 std::vector<uint8_t> expected_vec(kBytes, kBytes + kLen);
39 EXPECT_EQ(expected_vec, vectorFromBytes); 39 EXPECT_EQ(expected_vec, vectorFromBytes);
40 EXPECT_EQ(expected_vec, vectorFromVector); 40 EXPECT_EQ(expected_vec, vectorFromVector);
41 41
42 AppendJavaByteArrayToByteVector(env, bytes.obj(), &vectorFromBytes); 42 AppendJavaByteArrayToByteVector(env, bytes.obj(), &vectorFromBytes);
43 EXPECT_EQ(8U, vectorFromBytes.size()); 43 EXPECT_EQ(8U, vectorFromBytes.size());
44 expected_vec.insert(expected_vec.end(), kBytes, kBytes + kLen); 44 expected_vec.insert(expected_vec.end(), kBytes, kBytes + kLen);
45 EXPECT_EQ(expected_vec, vectorFromBytes); 45 EXPECT_EQ(expected_vec, vectorFromBytes);
46 } 46 }
47 47
48 void CheckBoolConversion(JNIEnv* env,
49 const bool* bool_array,
50 const size_t len,
51 const ScopedJavaLocalRef<jbooleanArray>& booleans) {
52 ASSERT_TRUE(booleans.obj());
53
54 jsize java_array_len = env->GetArrayLength(booleans.obj());
55 ASSERT_EQ(static_cast<jsize>(len), java_array_len);
56
57 jboolean value;
58 for (size_t i = 0; i < len; ++i) {
59 env->GetBooleanArrayRegion(booleans.obj(), i, 1, &value);
60 ASSERT_EQ(bool_array[i], value);
61 }
62 }
63
64 TEST(JniArray, BoolConversions) {
65 const bool kBools[] = {false, true, false};
66 const size_t kLen = arraysize(kBools);
67
68 JNIEnv* env = AttachCurrentThread();
69 CheckBoolConversion(env, kBools, kLen, ToJavaBooleanArray(env, kBools, kLen));
70 }
71
48 void CheckIntConversion( 72 void CheckIntConversion(
49 JNIEnv* env, 73 JNIEnv* env,
50 const int* int_array, 74 const int* int_array,
51 const size_t len, 75 const size_t len,
52 const ScopedJavaLocalRef<jintArray>& ints) { 76 const ScopedJavaLocalRef<jintArray>& ints) {
53 ASSERT_TRUE(ints.obj()); 77 ASSERT_TRUE(ints.obj());
54 78
55 jsize java_array_len = env->GetArrayLength(ints.obj()); 79 jsize java_array_len = env->GetArrayLength(ints.obj());
56 ASSERT_EQ(static_cast<jsize>(len), java_array_len); 80 ASSERT_EQ(static_cast<jsize>(len), java_array_len);
57 81
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 ScopedJavaLocalRef<jintArray> jints, 130 ScopedJavaLocalRef<jintArray> jints,
107 std::vector<int> int_vector, 131 std::vector<int> int_vector,
108 const size_t len) { 132 const size_t len) {
109 jint value; 133 jint value;
110 for (size_t i = 0; i < len; ++i) { 134 for (size_t i = 0; i < len; ++i) {
111 env->GetIntArrayRegion(jints.obj(), i, 1, &value); 135 env->GetIntArrayRegion(jints.obj(), i, 1, &value);
112 ASSERT_EQ(int_vector[i], value); 136 ASSERT_EQ(int_vector[i], value);
113 } 137 }
114 } 138 }
115 139
140 void CheckBoolArrayConversion(JNIEnv* env,
141 ScopedJavaLocalRef<jbooleanArray> jbooleans,
142 std::vector<bool> bool_vector,
143 const size_t len) {
144 jboolean value;
145 for (size_t i = 0; i < len; ++i) {
146 env->GetBooleanArrayRegion(jbooleans.obj(), i, 1, &value);
147 ASSERT_EQ(bool_vector[i], value);
148 }
149 }
150
116 void CheckFloatConversion( 151 void CheckFloatConversion(
117 JNIEnv* env, 152 JNIEnv* env,
118 const float* float_array, 153 const float* float_array,
119 const size_t len, 154 const size_t len,
120 const ScopedJavaLocalRef<jfloatArray>& floats) { 155 const ScopedJavaLocalRef<jfloatArray>& floats) {
121 ASSERT_TRUE(floats.obj()); 156 ASSERT_TRUE(floats.obj());
122 157
123 jsize java_array_len = env->GetArrayLength(floats.obj()); 158 jsize java_array_len = env->GetArrayLength(floats.obj());
124 ASSERT_EQ(static_cast<jsize>(len), java_array_len); 159 ASSERT_EQ(static_cast<jsize>(len), java_array_len);
125 160
126 jfloat value; 161 jfloat value;
127 for (size_t i = 0; i < len; ++i) { 162 for (size_t i = 0; i < len; ++i) {
128 env->GetFloatArrayRegion(floats.obj(), i, 1, &value); 163 env->GetFloatArrayRegion(floats.obj(), i, 1, &value);
129 ASSERT_EQ(float_array[i], value); 164 ASSERT_EQ(float_array[i], value);
130 } 165 }
131 } 166 }
132 167
133 TEST(JniArray, FloatConversions) { 168 TEST(JniArray, FloatConversions) {
134 const float kFloats[] = { 0.0f, 1.0f, -10.0f}; 169 const float kFloats[] = { 0.0f, 1.0f, -10.0f};
135 const size_t kLen = arraysize(kFloats); 170 const size_t kLen = arraysize(kFloats);
136 171
137 JNIEnv* env = AttachCurrentThread(); 172 JNIEnv* env = AttachCurrentThread();
138 CheckFloatConversion(env, kFloats, kLen, 173 CheckFloatConversion(env, kFloats, kLen,
139 ToJavaFloatArray(env, kFloats, kLen)); 174 ToJavaFloatArray(env, kFloats, kLen));
140 175
141 const std::vector<float> vec(kFloats, kFloats + kLen); 176 const std::vector<float> vec(kFloats, kFloats + kLen);
142 CheckFloatConversion(env, kFloats, kLen, ToJavaFloatArray(env, vec)); 177 CheckFloatConversion(env, kFloats, kLen, ToJavaFloatArray(env, vec));
143 } 178 }
144 179
180 TEST(JniArray, JavaBooleanArrayToBoolVector) {
181 const bool kBools[] = {false, true, false};
182 const size_t kLen = arraysize(kBools);
183
184 JNIEnv* env = AttachCurrentThread();
185 ScopedJavaLocalRef<jbooleanArray> jbooleans(env, env->NewBooleanArray(kLen));
186 ASSERT_TRUE(jbooleans.obj());
187
188 for (size_t i = 0; i < kLen; ++i) {
189 jboolean j = static_cast<jboolean>(kBools[i]);
190 env->SetBooleanArrayRegion(jbooleans.obj(), i, 1, &j);
191 ASSERT_FALSE(HasException(env));
192 }
193
194 std::vector<bool> bools;
195 JavaBooleanArrayToBoolVector(env, jbooleans.obj(), &bools);
196
197 ASSERT_EQ(static_cast<jsize>(bools.size()),
198 env->GetArrayLength(jbooleans.obj()));
199
200 CheckBoolArrayConversion(env, jbooleans, bools, kLen);
201 }
202
145 TEST(JniArray, JavaIntArrayToIntVector) { 203 TEST(JniArray, JavaIntArrayToIntVector) {
146 const int kInts[] = {0, 1, -1}; 204 const int kInts[] = {0, 1, -1};
147 const size_t kLen = arraysize(kInts); 205 const size_t kLen = arraysize(kInts);
148 206
149 JNIEnv* env = AttachCurrentThread(); 207 JNIEnv* env = AttachCurrentThread();
150 ScopedJavaLocalRef<jintArray> jints(env, env->NewIntArray(kLen)); 208 ScopedJavaLocalRef<jintArray> jints(env, env->NewIntArray(kLen));
151 ASSERT_TRUE(jints.obj()); 209 ASSERT_TRUE(jints.obj());
152 210
153 for (size_t i = 0; i < kLen; ++i) { 211 for (size_t i = 0; i < kLen; ++i) {
154 jint j = static_cast<jint>(kInts[i]); 212 jint j = static_cast<jint>(kInts[i]);
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 380
323 EXPECT_EQ(kNumItems, out.size()); 381 EXPECT_EQ(kNumItems, out.size());
324 CheckIntArrayConversion(env, int_array0, out[0], kLen0); 382 CheckIntArrayConversion(env, int_array0, out[0], kLen0);
325 CheckIntArrayConversion(env, int_array1, out[1], kLen1); 383 CheckIntArrayConversion(env, int_array1, out[1], kLen1);
326 CheckIntArrayConversion(env, int_array2, out[2], kLen2); 384 CheckIntArrayConversion(env, int_array2, out[2], kLen2);
327 CheckIntArrayConversion(env, int_array3, out[3], kLen3); 385 CheckIntArrayConversion(env, int_array3, out[3], kLen3);
328 } 386 }
329 387
330 } // namespace android 388 } // namespace android
331 } // namespace base 389 } // namespace base
OLDNEW
« no previous file with comments | « base/android/jni_array.cc ('k') | chrome/browser/android/chrome_backup_agent.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698