| 22 | namespace tflite { |
| 23 | |
| 24 | TEST(StringUtil, TestStringUtil) { |
| 25 | Interpreter interpreter; |
| 26 | interpreter.AddTensors(3); |
| 27 | |
| 28 | TfLiteTensor* t0 = interpreter.tensor(0); |
| 29 | t0->type = kTfLiteString; |
| 30 | t0->allocation_type = kTfLiteDynamic; |
| 31 | |
| 32 | TfLiteTensor* t1 = interpreter.tensor(1); |
| 33 | t1->type = kTfLiteString; |
| 34 | t1->allocation_type = kTfLiteDynamic; |
| 35 | |
| 36 | char data[] = {1, 0, 0, 0, 12, 0, 0, 0, 15, 0, 0, 0, 'X', 'Y', 'Z'}; |
| 37 | |
| 38 | TfLiteQuantization quant; |
| 39 | quant.type = kTfLiteNoQuantization; |
| 40 | quant.params = nullptr; |
| 41 | interpreter.SetTensorParametersReadOnly(2, kTfLiteString, "", {1}, quant, |
| 42 | data, 15); |
| 43 | TfLiteTensor* t2 = interpreter.tensor(2); |
| 44 | interpreter.AllocateTensors(); |
| 45 | |
| 46 | char s0[] = "ABC"; |
| 47 | string s1 = "DEFG"; |
| 48 | char s2[] = ""; |
| 49 | |
| 50 | // Write strings to tensors |
| 51 | DynamicBuffer buf0; |
| 52 | buf0.AddString(s0, 3); |
| 53 | DynamicBuffer buf1; |
| 54 | buf1.AddString(s1.data(), s1.length()); |
| 55 | buf0.AddString(s2, 0); |
| 56 | |
| 57 | auto new_shape = TfLiteIntArrayCreate(2); |
| 58 | new_shape->data[0] = 2; |
| 59 | new_shape->data[1] = 1; |
| 60 | buf0.WriteToTensor(t0, new_shape); |
| 61 | buf1.WriteToTensorAsVector(t1); |
| 62 | |
| 63 | // Check tensor shapes. |
| 64 | EXPECT_EQ(t0->dims->size, 2); |
| 65 | EXPECT_EQ(t0->dims->data[0], 2); |
| 66 | EXPECT_EQ(t0->dims->data[1], 1); |
| 67 | |
| 68 | EXPECT_EQ(t1->dims->size, 1); |
| 69 | EXPECT_EQ(t1->dims->data[0], 1); |
| 70 | |
| 71 | // Read strings from tensors. |
| 72 | ASSERT_EQ(GetStringCount(t0), 2); |
| 73 | StringRef str_ref; |
| 74 | str_ref = GetString(t0, 0); |
| 75 | ASSERT_EQ(string(str_ref.str, str_ref.len), "ABC"); |
| 76 | str_ref = GetString(t0, 1); |
| 77 | ASSERT_EQ(string(str_ref.str, str_ref.len), ""); |
| 78 | ASSERT_EQ(t0->bytes, 19); |
| 79 | |
| 80 | ASSERT_EQ(GetStringCount(t1), 1); |
| 81 | str_ref = GetString(t1, 0); |
nothing calls this directly
no test coverage detected