Welcome to mirror list, hosted at ThFree Co, Russian Federation.

draco_test_utils.cc « core « draco « src « dracoenc « draco « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a39e19f6b15138e955986fcff81fcc4bb4724064 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2016 The Draco Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "draco/core/draco_test_utils.h"

#include <fstream>

#include "draco/core/macros.h"
#include "draco_test_base.h"

namespace draco {

namespace {
static constexpr char kTestDataDir[] = DRACO_TEST_DATA_DIR;
static constexpr char kTestTempDir[] = DRACO_TEST_TEMP_DIR;
}  // namespace

std::string GetTestFileFullPath(const std::string &file_name) {
  return std::string(kTestDataDir) + std::string("/") + file_name;
}

std::string GetTestTempFileFullPath(const std::string &file_name) {
  return std::string(kTestTempDir) + std::string("/") + file_name;
}

bool GenerateGoldenFile(const std::string &golden_file_name, const void *data,
                        int data_size) {
  const std::string path = GetTestFileFullPath(golden_file_name);
  std::ofstream file(path, std::ios::binary);
  if (!file)
    return false;
  file.write(static_cast<const char *>(data), data_size);
  file.close();
  return true;
}

bool CompareGoldenFile(const std::string &golden_file_name, const void *data,
                       int data_size) {
  const std::string golden_path = GetTestFileFullPath(golden_file_name);
  std::ifstream in_file(golden_path, std::ios::binary);
  if (!in_file || data_size < 0)
    return false;
  const char *const data_c8 = static_cast<const char *>(data);
  constexpr int buffer_size = 1024;
  char buffer[buffer_size];
  size_t extracted_size = 0;
  size_t remaining_data_size = data_size;
  int offset = 0;
  while ((extracted_size = in_file.read(buffer, buffer_size).gcount()) > 0) {
    if (remaining_data_size <= 0)
      break;  // Input and golden sizes are different.
    size_t size_to_check = extracted_size;
    if (remaining_data_size < size_to_check)
      size_to_check = remaining_data_size;
    for (uint32_t i = 0; i < size_to_check; ++i) {
      if (buffer[i] != data_c8[offset++]) {
        LOG(INFO) << "Test output differed from golden file at byte "
                  << offset - 1;
        return false;
      }
    }
    remaining_data_size -= extracted_size;
  }
  if (remaining_data_size != extracted_size) {
    // Both of these values should be 0 at the end.
    LOG(INFO) << "Test output size differed from golden file size";
    return false;
  }
  return true;
}

}  // namespace draco