FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_encoding.c
1 /*
2 Author: Daniele Fognini
3 Copyright (C) 2013-2014, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <CUnit/CUnit.h>
22 #include <string_operations.h>
23 
24 #include "encoding.h"
25 #include "hash.h"
26 #include "libfocunit.h"
27 
28 
29 void test_guess_encoding() {
30  char* buffer = "an ascii text";
31  gchar* guessedEncoding = guessEncoding(buffer, strlen(buffer));
32 
33 #ifdef HAVE_CHARDET
34  CU_ASSERT_PTR_NULL(guessedEncoding);
35 #else
36  CU_ASSERT_PTR_NOT_NULL_FATAL(guessedEncoding);
37  FO_ASSERT_STRING_EQUAL(guessedEncoding, "us-ascii");
38 #endif
39 
40  if (guessedEncoding) {
41  g_free(guessedEncoding);
42  }
43 }
44 
45 void test_guess_encodingUtf8() {
46  char* buffer = "an utf8 ß";
47  gchar* guessedEncoding = guessEncoding(buffer, strlen(buffer));
48 
49  CU_ASSERT_PTR_NOT_NULL_FATAL(guessedEncoding);
50 
51 #ifdef HAVE_CHARDET
52  FO_ASSERT_STRING_EQUAL(guessedEncoding, "UTF-8");
53 #else
54  FO_ASSERT_STRING_EQUAL(guessedEncoding, "utf-8");
55 #endif
56 
57  if (guessedEncoding) {
58  g_free(guessedEncoding);
59  }
60 }
61 
62 void test_guess_encodingLatin1() {
63  char* buffer = "a latin1 \xdf\x0a"; // ß
64  gchar* guessedEncoding = guessEncoding(buffer, strlen(buffer));
65 
66  CU_ASSERT_PTR_NOT_NULL_FATAL(guessedEncoding);
67 
68 #ifdef HAVE_CHARDET
69  FO_ASSERT_STRING_EQUAL(guessedEncoding, "windows-1252");
70 #else
71  FO_ASSERT_STRING_EQUAL(guessedEncoding, "iso-8859-1");
72 #endif
73 
74  if (guessedEncoding) {
75  g_free(guessedEncoding);
76  }
77 }
78 
79 CU_TestInfo encoding_testcases[] = {
80  {"Testing guessing encoding of buffer:", test_guess_encoding},
81  {"Testing guessing encoding of buffer utf8:", test_guess_encodingUtf8},
82  {"Testing guessing encoding of buffer Latin1:", test_guess_encodingLatin1},
83  CU_TEST_INFO_NULL
84 };
char buffer[2048]
The last thing received from the scheduler.