@@ -48,30 +48,51 @@ StatusOr<ImageData> LoadImage(const char* image_name) {
4848
4949class ImageClassifierFromOptionsTest : public tflite_shims ::testing::Test {};
5050
51- TEST_F (ImageClassifierFromOptionsTest, FailsWithMissingModelPath) {
52- TfLiteImageClassifierOptions options = TfLiteImageClassifierOptionsCreate ();
53-
54- TfLiteSupportError *error = nullptr ;
51+ TEST_F (ImageClassifierFromOptionsTest, FailsWithNullOptionsAndError) {
52+ TfLiteSupportError* error = nullptr ;
5553 TfLiteImageClassifier* image_classifier =
56- TfLiteImageClassifierFromOptions (&options, &error);
54+ TfLiteImageClassifierFromOptions (nullptr , &error);
55+
5756 EXPECT_EQ (image_classifier, nullptr );
57+ if (image_classifier) TfLiteImageClassifierDelete (image_classifier);
58+
5859 ASSERT_NE (error, nullptr );
5960 EXPECT_EQ (error->code , kInvalidArgumentError );
6061 EXPECT_NE (error->message , nullptr );
62+ EXPECT_NE (strstr (error->message , " Expected non null options" ), nullptr );
63+
6164 TfLiteSupportErrorDelete (error);
6265}
6366
64- TEST_F (ImageClassifierFromOptionsTest, FailsWithMissingModelPathAndNullError ) {
65- TfLiteImageClassifierOptions options = {{{ 0 }}} ;
67+ TEST_F (ImageClassifierFromOptionsTest, FailsWithMissingModelPath ) {
68+ TfLiteImageClassifierOptions options = TfLiteImageClassifierOptionsCreate () ;
6669 TfLiteImageClassifier* image_classifier =
6770 TfLiteImageClassifierFromOptions (&options, nullptr );
6871 EXPECT_EQ (image_classifier, nullptr );
72+ if (image_classifier) TfLiteImageClassifierDelete (image_classifier);
73+ }
74+
75+ TEST_F (ImageClassifierFromOptionsTest, FailsWithMissingModelPathAndError) {
76+ TfLiteImageClassifierOptions options = TfLiteImageClassifierOptionsCreate ();
77+
78+ TfLiteSupportError* error = nullptr ;
79+ TfLiteImageClassifier* image_classifier =
80+ TfLiteImageClassifierFromOptions (&options, &error);
81+
82+ EXPECT_EQ (image_classifier, nullptr );
83+ if (image_classifier) TfLiteImageClassifierDelete (image_classifier);
84+
85+ ASSERT_NE (error, nullptr );
86+ EXPECT_EQ (error->code , kInvalidArgumentError );
87+ EXPECT_NE (error->message , nullptr );
88+ EXPECT_NE (strstr (error->message , " `base_options.model_file`" ), nullptr );
89+
90+ TfLiteSupportErrorDelete (error);
6991}
7092
7193TEST_F (ImageClassifierFromOptionsTest, SucceedsWithModelPath) {
72- std::string model_path =
73- JoinPath (" ./" /* test src dir*/ , kTestDataDirectory ,
74- kMobileNetQuantizedWithMetadata );
94+ std::string model_path = JoinPath (" ./" /* test src dir*/ , kTestDataDirectory ,
95+ kMobileNetQuantizedWithMetadata );
7596 TfLiteImageClassifierOptions options = TfLiteImageClassifierOptionsCreate ();
7697 options.base_options .model_file .file_path = model_path.data ();
7798 TfLiteImageClassifier* image_classifier =
@@ -81,29 +102,28 @@ TEST_F(ImageClassifierFromOptionsTest, SucceedsWithModelPath) {
81102 TfLiteImageClassifierDelete (image_classifier);
82103}
83104
84- TEST_F (ImageClassifierFromOptionsTest, SucceedsWithNumberOfThreads) {
85- std::string model_path =
86- JoinPath (" ./" /* test src dir*/ , kTestDataDirectory ,
87- kMobileNetQuantizedWithMetadata );
105+ TEST_F (ImageClassifierFromOptionsTest, SucceedsWithNumberOfThreadsAndError) {
106+ std::string model_path = JoinPath (" ./" /* test src dir*/ , kTestDataDirectory ,
107+ kMobileNetQuantizedWithMetadata );
88108 TfLiteImageClassifierOptions options = TfLiteImageClassifierOptionsCreate ();
89109 options.base_options .model_file .file_path = model_path.data ();
90110 options.base_options .compute_settings .cpu_settings .num_threads = 3 ;
91111
92- TfLiteSupportError * error = nullptr ;
112+ TfLiteSupportError* error = nullptr ;
93113 TfLiteImageClassifier* image_classifier =
94114 TfLiteImageClassifierFromOptions (&options, &error);
95115
96116 EXPECT_NE (image_classifier, nullptr );
97117 EXPECT_EQ (error, nullptr );
98- TfLiteImageClassifierDelete (image_classifier);
118+
119+ if (image_classifier) TfLiteImageClassifierDelete (image_classifier);
99120 if (error) TfLiteSupportErrorDelete (error);
100121}
101122
102123TEST_F (ImageClassifierFromOptionsTest,
103- FailsWithClassNameDenyListAndClassNameAllowList) {
104- std::string model_path =
105- JoinPath (" ./" /* test src dir*/ , kTestDataDirectory ,
106- kMobileNetQuantizedWithMetadata );
124+ FailsWithClassNameDenyListAndClassNameAllowListAndError) {
125+ std::string model_path = JoinPath (" ./" /* test src dir*/ , kTestDataDirectory ,
126+ kMobileNetQuantizedWithMetadata );
107127
108128 TfLiteImageClassifierOptions options = TfLiteImageClassifierOptionsCreate ();
109129 options.base_options .model_file .file_path = model_path.data ();
@@ -126,6 +146,32 @@ TEST_F(ImageClassifierFromOptionsTest,
126146 ASSERT_NE (error, nullptr );
127147 EXPECT_EQ (error->code , kInvalidArgumentError );
128148 EXPECT_NE (error->message , nullptr );
149+ EXPECT_NE (strstr (error->message , " mutually exclusive options" ), nullptr );
150+
151+ TfLiteSupportErrorDelete (error);
152+ }
153+
154+ TEST (ImageClassifierNullClassifierClassifyTest,
155+ FailsWithNullImageClassifierAndError) {
156+ SUPPORT_ASSERT_OK_AND_ASSIGN (ImageData image_data,
157+ LoadImage (" burger-224.png" ));
158+
159+ TfLiteSupportError* error = nullptr ;
160+ TfLiteClassificationResult* classification_result =
161+ TfLiteImageClassifierClassify (nullptr , nullptr , &error);
162+
163+ ImageDataFree (&image_data);
164+
165+ EXPECT_EQ (classification_result, nullptr );
166+ if (classification_result)
167+ TfLiteClassificationResultDelete (classification_result);
168+
169+ ASSERT_NE (error, nullptr );
170+ EXPECT_EQ (error->code , kInvalidArgumentError );
171+ EXPECT_NE (error->message , nullptr );
172+ EXPECT_NE (strstr (error->message , " Expected non null image classifier" ),
173+ nullptr );
174+
129175 TfLiteSupportErrorDelete (error);
130176}
131177
@@ -157,24 +203,66 @@ TEST_F(ImageClassifierClassifyTest, SucceedsWithImageData) {
157203
158204 TfLiteSupportError* error = nullptr ;
159205 TfLiteClassificationResult* classification_result =
160- TfLiteImageClassifierClassify (image_classifier, &frame_buffer, &error );
206+ TfLiteImageClassifierClassify (image_classifier, &frame_buffer, nullptr );
161207
162208 ImageDataFree (&image_data);
163209
164- EXPECT_EQ (error, nullptr );
165- if (error) TfLiteSupportErrorDelete (error);
166-
167210 ASSERT_NE (classification_result, nullptr );
168211 EXPECT_GE (classification_result->size , 1 );
169212 EXPECT_NE (classification_result->classifications , nullptr );
170213 EXPECT_GE (classification_result->classifications ->size , 1 );
171214 EXPECT_NE (classification_result->classifications ->categories , nullptr );
172-
173215 // TODO(prianka): check score and labels`
174216
175217 TfLiteClassificationResultDelete (classification_result);
176218}
177219
220+ TEST_F (ImageClassifierClassifyTest, FailsWithNullFrameBufferAndError) {
221+ SUPPORT_ASSERT_OK_AND_ASSIGN (ImageData image_data,
222+ LoadImage (" burger-224.png" ));
223+
224+ TfLiteSupportError* error = nullptr ;
225+ TfLiteClassificationResult* classification_result =
226+ TfLiteImageClassifierClassify (image_classifier, nullptr , &error);
227+
228+ ImageDataFree (&image_data);
229+
230+ EXPECT_EQ (classification_result, nullptr );
231+ if (classification_result)
232+ TfLiteClassificationResultDelete (classification_result);
233+
234+ ASSERT_NE (error, nullptr );
235+ EXPECT_EQ (error->code , kInvalidArgumentError );
236+ EXPECT_NE (error->message , nullptr );
237+ EXPECT_NE (strstr (error->message , " Expected non null frame buffer" ), nullptr );
238+
239+ TfLiteSupportErrorDelete (error);
240+ }
241+
242+ TEST_F (ImageClassifierClassifyTest, FailsWithNullImageDataAndError) {
243+ SUPPORT_ASSERT_OK_AND_ASSIGN (ImageData image_data,
244+ LoadImage (" burger-224.png" ));
245+
246+ TfLiteFrameBuffer frame_buffer = {.format = kRGB , .orientation = kTopLeft };
247+
248+ TfLiteSupportError* error = nullptr ;
249+ TfLiteClassificationResult* classification_result =
250+ TfLiteImageClassifierClassify (image_classifier, &frame_buffer, &error);
251+
252+ ImageDataFree (&image_data);
253+
254+ EXPECT_EQ (classification_result, nullptr );
255+ if (classification_result)
256+ TfLiteClassificationResultDelete (classification_result);
257+
258+ ASSERT_NE (error, nullptr );
259+ EXPECT_EQ (error->code , kInvalidArgumentError );
260+ EXPECT_NE (error->message , nullptr );
261+ EXPECT_NE (strstr (error->message , " Invalid stride information" ), nullptr );
262+
263+ TfLiteSupportErrorDelete (error);
264+ }
265+
178266TEST_F (ImageClassifierClassifyTest, SucceedsWithRoiWithinImageBounds) {
179267 SUPPORT_ASSERT_OK_AND_ASSIGN (ImageData image_data,
180268 LoadImage (" burger-224.png" ));
@@ -194,9 +282,6 @@ TEST_F(ImageClassifierClassifyTest, SucceedsWithRoiWithinImageBounds) {
194282
195283 ImageDataFree (&image_data);
196284
197- EXPECT_EQ (error, nullptr );
198- if (error) TfLiteSupportErrorDelete (error);
199-
200285 ASSERT_NE (classification_result, nullptr );
201286 EXPECT_GE (classification_result->size , 1 );
202287 EXPECT_NE (classification_result->classifications , nullptr );
@@ -207,7 +292,7 @@ TEST_F(ImageClassifierClassifyTest, SucceedsWithRoiWithinImageBounds) {
207292 TfLiteClassificationResultDelete (classification_result);
208293}
209294
210- TEST_F (ImageClassifierClassifyTest, FailsWithRoiOutsideImageBounds ) {
295+ TEST_F (ImageClassifierClassifyTest, FailsWithRoiOutsideImageBoundsAndError ) {
211296 SUPPORT_ASSERT_OK_AND_ASSIGN (ImageData image_data,
212297 LoadImage (" burger-224.png" ));
213298
@@ -226,22 +311,23 @@ TEST_F(ImageClassifierClassifyTest, FailsWithRoiOutsideImageBounds) {
226311
227312 ImageDataFree (&image_data);
228313
229- EXPECT_NE (error, nullptr );
230- EXPECT_EQ (error->code , kInvalidArgumentError );
231- EXPECT_NE (error->message , nullptr );
232- TfLiteSupportErrorDelete (error);
233-
234314 EXPECT_EQ (classification_result, nullptr );
235315 if (classification_result)
236316 TfLiteClassificationResultDelete (classification_result);
317+
318+ ASSERT_NE (error, nullptr );
319+ EXPECT_EQ (error->code , kInvalidArgumentError );
320+ EXPECT_NE (error->message , nullptr );
321+ EXPECT_NE (strstr (error->message , " Invalid crop coordinates" ), nullptr );
322+
323+ TfLiteSupportErrorDelete (error);
237324}
238325
239326TEST (ImageClassifierWithUserDefinedOptionsClassifyTest,
240327 SucceedsWithClassNameDenyList) {
241328 const char * denylisted_label_name = " cheeseburger" ;
242- std::string model_path =
243- JoinPath (" ./" /* test src dir*/ , kTestDataDirectory ,
244- kMobileNetQuantizedWithMetadata );
329+ std::string model_path = JoinPath (" ./" /* test src dir*/ , kTestDataDirectory ,
330+ kMobileNetQuantizedWithMetadata );
245331
246332 TfLiteImageClassifierOptions options = TfLiteImageClassifierOptionsCreate ();
247333 options.base_options .model_file .file_path = model_path.data ();
@@ -267,6 +353,7 @@ TEST(ImageClassifierWithUserDefinedOptionsClassifyTest,
267353 TfLiteImageClassifierClassify (image_classifier, &frame_buffer, nullptr );
268354
269355 ImageDataFree (&image_data);
356+ if (image_classifier) TfLiteImageClassifierDelete (image_classifier);
270357
271358 ASSERT_NE (classification_result, nullptr );
272359 EXPECT_GE (classification_result->size , 1 );
@@ -277,18 +364,15 @@ TEST(ImageClassifierWithUserDefinedOptionsClassifyTest,
277364 denylisted_label_name),
278365 0 );
279366
280- if (image_classifier) TfLiteImageClassifierDelete (image_classifier);
281-
282367 TfLiteClassificationResultDelete (classification_result);
283368}
284369
285370TEST (ImageClassifierWithUserDefinedOptionsClassifyTest,
286371 SucceedsWithClassNameAllowList) {
287372 const char * allowlisted_label_name = " cheeseburger" ;
288- std::string model_path =
289- JoinPath (" ./" /* test src dir*/ , kTestDataDirectory ,
290- kMobileNetQuantizedWithMetadata )
291- .data ();
373+ std::string model_path = JoinPath (" ./" /* test src dir*/ , kTestDataDirectory ,
374+ kMobileNetQuantizedWithMetadata )
375+ .data ();
292376
293377 TfLiteImageClassifierOptions options = TfLiteImageClassifierOptionsCreate ();
294378 options.base_options .model_file .file_path = model_path.data ();
@@ -314,6 +398,7 @@ TEST(ImageClassifierWithUserDefinedOptionsClassifyTest,
314398 TfLiteImageClassifierClassify (image_classifier, &frame_buffer, nullptr );
315399
316400 ImageDataFree (&image_data);
401+ if (image_classifier) TfLiteImageClassifierDelete (image_classifier);
317402
318403 ASSERT_NE (classification_result, nullptr );
319404 EXPECT_GE (classification_result->size , 1 );
@@ -324,8 +409,6 @@ TEST(ImageClassifierWithUserDefinedOptionsClassifyTest,
324409 allowlisted_label_name),
325410 0 );
326411
327- if (image_classifier) TfLiteImageClassifierDelete (image_classifier);
328-
329412 TfLiteClassificationResultDelete (classification_result);
330413}
331414
0 commit comments