aboutsummaryrefslogtreecommitdiffstats
path: root/tests/check_set.c
blob: 29e3c8c88fe571786fcb6c0d8a9270ba118c80fe (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdlib.h>
#include <glib.h>
#include <gmodule.h>
#include <check.h>
#include <string.h>
#include "set.h"
#include "testsuite.h"

START_TEST(test_set_add)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "default", NULL, data);
	fail_unless(s == t);
	fail_unless(t->data == data);
	fail_unless(strcmp(t->def, "default") == 0);
END_TEST

START_TEST(test_set_add_existing)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "default", NULL, data);
	t = set_add(&s, "name", "newdefault", NULL, data);
	fail_unless(s == t);
	fail_unless(strcmp(t->def, "newdefault") == 0);
END_TEST

START_TEST(test_set_find_unknown)
	set_t *s = NULL, *t;
	fail_unless (set_find(&s, "foo") == NULL);
END_TEST

START_TEST(test_set_find)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "default", NULL, data);
	fail_unless(s == t);
	fail_unless(set_find(&s, "name") == t);
END_TEST

START_TEST(test_set_get_str_default)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "default", NULL, data);
	fail_unless(s == t);
	fail_unless(strcmp(set_getstr(&s, "name"), "default") == 0);
END_TEST

START_TEST(test_set_get_bool_default)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "true", NULL, data);
	fail_unless(s == t);
	fail_unless(set_getbool(&s, "name"));
END_TEST

START_TEST(test_set_get_bool_integer)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "3", NULL, data);
	fail_unless(s == t);
	fail_unless(set_getbool(&s, "name") == 3);
END_TEST

START_TEST(test_set_get_bool_unknown)
	set_t *s = NULL;
	fail_unless(set_getbool(&s, "name") == 0);
END_TEST

START_TEST(test_set_get_str_value)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "default", NULL, data);
	set_setstr(&s, "name", "foo");
	fail_unless(strcmp(set_getstr(&s, "name"), "foo") == 0);
END_TEST

START_TEST(test_set_get_str_unknown)
	set_t *s = NULL;
	fail_unless(set_getstr(&s, "name") == NULL);
END_TEST

START_TEST(test_setint)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "10", NULL, data);
	set_setint(&s, "name", 3);
	fail_unless(set_getint(&s, "name") == 3);
END_TEST

START_TEST(test_setstr)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "foo", NULL, data);
	set_setstr(&s, "name", "bloe");
	fail_unless(strcmp(set_getstr(&s, "name"), "bloe") == 0);
END_TEST

START_TEST(test_set_get_int_unknown)
	set_t *s = NULL;
	fail_unless(set_getint(&s, "foo") == 0);
END_TEST

Suite *set_suite (void)
{
	Suite *s = suite_create("Set");
	TCase *tc_core = tcase_create("Core");
	suite_add_tcase (s, tc_core);
	tcase_add_test (tc_core, test_set_add);
	tcase_add_test (tc_core, test_set_add_existing);
	tcase_add_test (tc_core, test_set_find_unknown);
	tcase_add_test (tc_core, test_set_find);
	tcase_add_test (tc_core, test_set_get_str_default);
	tcase_add_test (tc_core, test_set_get_str_value);
	tcase_add_test (tc_core, test_set_get_str_unknown);
	tcase_add_test (tc_core, test_set_get_bool_default);
	tcase_add_test (tc_core, test_set_get_bool_integer);
	tcase_add_test (tc_core, test_set_get_bool_unknown);
	tcase_add_test (tc_core, test_set_get_int_unknown);
	tcase_add_test (tc_core, test_setint);
	tcase_add_test (tc_core, test_setstr);
	return s;
}
{@filename} was lost." if $DEBUG clear end self end def load(arg) if arg.kind_of? String begin st = File.stat(arg) @last_modified = [st.ctime, st.mtime] rescue Exception end load_from_file(arg) else load_from_stream(arg) end @filename = arg self end def load_from_stream(io) magic = io.read(4) case magic when MAGIC_BIG_ENDIAN @little_endian = false when MAGIC_LITTLE_ENDIAN @little_endian = true else raise InvalidFormat.new(sprintf("Unknown signature %s", magic.dump)) end endian_type6 = @little_endian ? 'V6' : 'N6' endian_type_astr = @little_endian ? 'V*' : 'N*' header = HeaderRev1.new(magic, *(io.read(4 * 6).unpack(endian_type6))) if header.revision == 1 # FIXME: It doesn't support sysdep correctly. header.n_sysdep_segments = io.read(4).unpack(endian_type6) header.sysdep_segments_offset = io.read(4).unpack(endian_type6) header.n_sysdep_strings = io.read(4).unpack(endian_type6) header.orig_sysdep_tab_offset = io.read(4).unpack(endian_type6) header.trans_sysdep_tab_offset = io.read(4).unpack(endian_type6) elsif header.revision > 1 raise InvalidFormat.new(sprintf("file format revision %d isn't supported", header.revision)) end io.pos = header.orig_table_offset orig_table_data = io.read((4 * 2) * header.nstrings).unpack(endian_type_astr) io.pos = header.translated_table_offset trans_table_data = io.read((4 * 2) * header.nstrings).unpack(endian_type_astr) original_strings = Array.new(header.nstrings) for i in 0...header.nstrings io.pos = orig_table_data[i * 2 + 1] original_strings[i] = io.read(orig_table_data[i * 2 + 0]) end clear for i in 0...header.nstrings io.pos = trans_table_data[i * 2 + 1] str = io.read(trans_table_data[i * 2 + 0]) if (! original_strings[i]) || original_strings[i] == "" if str @charset = nil @nplurals = nil @plural = nil str.each_line{|line| if /^Content-Type:/i =~ line and /charset=((?:\w|-)+)/i =~ line @charset = $1 elsif /^Plural-Forms:\s*nplurals\s*\=\s*(\d*);\s*plural\s*\=\s*([^;]*)\n?/ =~ line @nplurals = $1 @plural = $2 end break if @charset and @nplurals } @nplurals = "1" unless @nplurals @plural = "0" unless @plural end else if @output_charset begin str = Iconv.conv(@output_charset, @charset, str) if @charset rescue Iconv::Failure if $DEBUG warn "@charset = ", @charset warn"@output_charset = ", @output_charset warn "msgid = ", original_strings[i] warn "msgstr = ", str end end end end self[original_strings[i]] = str.freeze end self end def prime?(number) ('1' * number) !~ /^1?$|^(11+?)\1+$/ end begin require 'prime' def next_prime(seed) Prime.instance.find{|x| x > seed } end rescue LoadError def next_prime(seed) require 'mathn' prime = Prime.new while current = prime.succ return current if current > seed end end end HASHWORDBITS = 32 # From gettext-0.12.1/gettext-runtime/intl/hash-string.h # Defines the so called `hashpjw' function by P.J. Weinberger # [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools, # 1986, 1987 Bell Telephone Laboratories, Inc.] def hash_string(str) hval = 0 i = 0 str.each_byte do |b| break if b == '\0' hval <<= 4 hval += b.to_i g = hval & (0xf << (HASHWORDBITS - 4)) if (g != 0) hval ^= g >> (HASHWORDBITS - 8) hval ^= g end end hval end #Save data as little endian format. def save_to_stream(io) header_size = 4 * 7 table_size = 4 * 2 * size hash_table_size = next_prime((size * 4) / 3) hash_table_size = 3 if hash_table_size <= 2 header = Header.new( MAGIC_LITTLE_ENDIAN, # magic 0, # revision size, # nstrings header_size, # orig_table_offset header_size + table_size, # translated_table_offset hash_table_size, # hash_table_size header_size + table_size * 2 # hash_table_offset ) io.write(header.to_a.pack('a4V*')) ary = to_a ary.sort!{|a, b| a[0] <=> b[0]} # sort by original string pos = header.hash_table_size * 4 + header.hash_table_offset orig_table_data = Array.new() ary.each{|item, _| orig_table_data.push(item.bytesize) orig_table_data.push(pos) pos += item.bytesize + 1 # +1 is <NUL> } io.write(orig_table_data.pack('V*')) trans_table_data = Array.new() ary.each{|_, item| trans_table_data.push(item.bytesize) trans_table_data.push(pos) pos += item.bytesize + 1 # +1 is <NUL> } io.write(trans_table_data.pack('V*')) hash_tab = Array.new(hash_table_size) j = 0 ary[0...size].each {|key, _| hash_val = hash_string(key) idx = hash_val % hash_table_size if hash_tab[idx] != nil incr = 1 + (hash_val % (hash_table_size - 2)) begin if (idx >= hash_table_size - incr) idx -= hash_table_size - incr else idx += incr end end until (hash_tab[idx] == nil) end hash_tab[idx] = j + 1 j += 1 } hash_tab.collect!{|i| i ? i : 0} io.write(hash_tab.pack('V*')) ary.each{|item, _| io.write(item); io.write("\0") } ary.each{|_, item| io.write(item); io.write("\0") } self end def load_from_file(filename) @filename = filename begin File.open(filename, 'rb'){|f| load_from_stream(f)} rescue => e e.set_backtrace("File: #{@filename}") raise e end end def save_to_file(filename) File.open(filename, 'wb'){|f| save_to_stream(f)} end def set_comment(msgid_or_sym, comment) #Do nothing end def plural_as_proc unless @plural_proc @plural_proc = Proc.new{|n| eval(@plural)} begin @plural_proc.call(1) rescue @plural_proc = Proc.new{|n| 0} end end @plural_proc end attr_accessor :little_endian, :path, :last_modified attr_reader :charset, :nplurals, :plural end end # Test if $0 == __FILE__ if (ARGV.include? "-h") or (ARGV.include? "--help") STDERR.puts("mo.rb [filename.mo ...]") exit end ARGV.each{ |item| mo = GetText::MOFile.open(item) puts "------------------------------------------------------------------" puts "charset = \"#{mo.charset}\"" puts "nplurals = \"#{mo.nplurals}\"" puts "plural = \"#{mo.plural}\"" puts "------------------------------------------------------------------" mo.each do |key, value| puts "original message = #{key.inspect}" puts "translated message = #{value.inspect}" puts "--------------------------------------------------------------------" end } end