Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright 2011 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "test/test_utils.h"
     18 
     19 #include <stdio.h>
     20 #include <unicode/ucnv.h>
     21 #include <unicode/uchar.h>
     22 
     23 #include "gtest/gtest.h"
     24 #include "sfntly/font.h"
     25 #include "sfntly/data/memory_byte_array.h"
     26 #include "sfntly/data/growable_memory_byte_array.h"
     27 #include "sfntly/port/file_input_stream.h"
     28 
     29 namespace sfntly {
     30 TestUtils::TestUtils() {}
     31 
     32 // static
     33 // OutputStream CreateOutputStream(const char *file_path) {
     34 // }
     35 
     36 // static
     37 // void TestUtils::CreateNewFile(const char* file_path) {
     38 // }
     39 
     40 // static
     41 int32_t TestUtils::EncodeOneChar(UConverter* encoder, int16_t uchar) {
     42   char* target = new char[ucnv_getMaxCharSize(encoder) * 2];
     43   char* target_end;
     44   UChar* source = new UChar[2];
     45   UChar* source_end;
     46   source[0] = (UChar)uchar;
     47   source[1] = 0;
     48   UErrorCode status = U_ZERO_ERROR;
     49   source_end = source;
     50   target_end = target;
     51   ucnv_fromUnicode(encoder, &target_end, target + 4,
     52                    (const UChar**)&source_end, source + sizeof(UChar),
     53                    NULL, TRUE, &status);
     54   if (!U_SUCCESS(status)) {
     55     fprintf(stderr, "Error occured in conversion of %d: %s\n",
     56             uchar, u_errorName(status));
     57     delete[] source;
     58     delete[] target;
     59     return 0;
     60   }
     61   int32_t enc_char = 0;
     62   for (int32_t position = 0; position < target_end - target; ++position) {
     63     enc_char <<= 8;
     64     enc_char |= (target[position] & 0xff);
     65   }
     66   delete[] source;
     67   delete[] target;
     68   return enc_char;
     69 }
     70 
     71 // static
     72 UConverter* TestUtils::GetEncoder(const char* charset_name) {
     73   if (charset_name == NULL || strcmp(charset_name, "") == 0)
     74     return NULL;
     75   UErrorCode status = U_ZERO_ERROR;
     76   UConverter* conv = ucnv_open(charset_name, &status);
     77   // if (!U_SUCCESS(status))
     78   //   return NULL;
     79   return conv;  // returns NULL @ error anyway
     80 }
     81 
     82 // Get a file's extension
     83 // static
     84 const char* TestUtils::Extension(const char* file_path) {
     85   if (!file_path)
     86     return NULL;
     87   return strrchr(file_path, EXTENSION_SEPARATOR);
     88 }
     89 }
     90