| 29 | using singa::Shape; |
| 30 | using singa::Tensor; |
| 31 | TEST(CSV, EncoderDecode) { |
| 32 | singa::CSVEncoder encoder; |
| 33 | singa::CSVDecoder decoder; |
| 34 | |
| 35 | singa::DecoderConf decoder_conf; |
| 36 | decoder_conf.set_has_label(true); |
| 37 | decoder.Setup(decoder_conf); |
| 38 | EXPECT_EQ(true, decoder.has_label()); |
| 39 | |
| 40 | float in_data[] = {1.23f, 4.5f, 5.1f, 3.33f, 0.44f}; |
| 41 | std::string in_str = "2, 1.23, 4.5, 5.1, 3.33, 0.44"; |
| 42 | int in_label = 2; |
| 43 | size_t size = 5; |
| 44 | |
| 45 | std::vector<Tensor> input; |
| 46 | Tensor data(Shape{size}, singa::kFloat32), label(Shape{1}, singa::kInt); |
| 47 | data.CopyDataFromHostPtr<float>(in_data, size); |
| 48 | label.CopyDataFromHostPtr<int>(&in_label, 1); |
| 49 | input.push_back(data); |
| 50 | input.push_back(label); |
| 51 | |
| 52 | std::string value = encoder.Encode(input); |
| 53 | in_str.erase(std::remove(in_str.begin(), in_str.end(), ' '), in_str.end()); |
| 54 | EXPECT_EQ(in_str, value); |
| 55 | |
| 56 | std::vector<Tensor> output = decoder.Decode(value); |
| 57 | const auto* out_data = output.at(0).data<float>(); |
| 58 | const auto* out_label = output.at(1).data<int>(); |
| 59 | for (size_t i = 0; i < size; i++) EXPECT_EQ(in_data[i], out_data[i]); |
| 60 | EXPECT_EQ(in_label, out_label[0]); |
| 61 | } |