aboutsummaryrefslogtreecommitdiff
path: root/cpp/src/test/test_utils.cc
blob: 4751b926251f8fe6724afb1f0b06884513010e8f (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
84
85
86
87
88
89
/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * 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 "test/test_utils.h"

#include <stdio.h>
#include <unicode/ucnv.h>
#include <unicode/uchar.h>

#include "gtest/gtest.h"
#include "sfntly/font.h"
#include "sfntly/data/memory_byte_array.h"
#include "sfntly/data/growable_memory_byte_array.h"
#include "sfntly/port/file_input_stream.h"

namespace sfntly {
TestUtils::TestUtils() {}

// static
// OutputStream CreateOutputStream(const char *file_path) {
// }

// static
// void TestUtils::CreateNewFile(const char* file_path) {
// }

// static
int32_t TestUtils::EncodeOneChar(UConverter* encoder, int16_t uchar) {
  char* target = new char[ucnv_getMaxCharSize(encoder) * 2];
  char* target_end;
  UChar* source = new UChar[2];
  UChar* source_end;
  source[0] = (UChar)uchar;
  source[1] = 0;
  UErrorCode status = U_ZERO_ERROR;
  source_end = source;
  target_end = target;
  ucnv_fromUnicode(encoder, &target_end, target + 4,
                   (const UChar**)&source_end, source + sizeof(UChar),
                   NULL, TRUE, &status);
  if (!U_SUCCESS(status)) {
    fprintf(stderr, "Error occured in conversion of %d: %s\n",
            uchar, u_errorName(status));
    delete[] source;
    delete[] target;
    return 0;
  }
  int32_t enc_char = 0;
  for (int32_t position = 0; position < target_end - target; ++position) {
    enc_char <<= 8;
    enc_char |= (target[position] & 0xff);
  }
  delete[] source;
  delete[] target;
  return enc_char;
}

// static
UConverter* TestUtils::GetEncoder(const char* charset_name) {
  if (charset_name == NULL || strcmp(charset_name, "") == 0)
    return NULL;
  UErrorCode status = U_ZERO_ERROR;
  UConverter* conv = ucnv_open(charset_name, &status);
  // if (!U_SUCCESS(status))
  //   return NULL;
  return conv;  // returns NULL @ error anyway
}

// Get a file's extension
// static
const char* TestUtils::Extension(const char* file_path) {
  if (!file_path)
    return NULL;
  return strrchr(file_path, EXTENSION_SEPARATOR);
}
}