3 * Copyright (C) 2012 Maciej Piechotka
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Maciej Piechotka <uzytkownik2@gmail.com>
22 public class TestData {
23 private static string?[] ones = {null, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
24 private static string[] tens = {null, null, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
25 private static string hundred = "hundred";
26 private static string?[] thousands = {null, "thousand", "million", "billion", "trillion"};
28 private static uint data_size () {
29 return Test.quick() ? 128 : 1024;
32 private static uint DATA_SIZE = data_size ();
33 private static const uint RND_IDX_SIZE = 8;
35 private static string[] data = create_data (DATA_SIZE);
36 private static string[] sorted_data = sort_array (data);
37 private static uint[] random_idx = draw_numbers(DATA_SIZE, RND_IDX_SIZE);
39 public static unowned string[] get_data () {
40 TypeClass klass = typeof (TestData).class_ref ();
45 public static unowned string[] get_sorted_data () {
46 TypeClass klass = typeof (TestData).class_ref ();
51 public static unowned uint[] get_drawn_numbers () {
52 TypeClass klass = typeof (TestData).class_ref ();
57 private static uint[] draw_numbers (uint n, uint k) {
58 uint[] result = new uint[n];
60 for (uint i = 0; i < n; i++) {
63 // Fisher-Yates shuffle algorithm. Possibly not the most efficient implementation but oh well
64 for (uint i = n - 1; i >= 1; i--) {
65 int j = Test.rand_int_range (0, (int32)(i + 1));
67 result[i] = result[j];
70 result.resize ((int)k);
74 private static string print3digit (uint n) {
75 string? h = (n >= 200) ? "%s %s".printf(ones[n / 100], hundred) : ((n >= 100) ? hundred : null);
77 unowned string? t = tens[n / 10];
78 n = (n >= ones.length) ? n % 10 : n;
79 unowned string? o = ones[n];
80 return "%s%s%s%s%s".printf(h != null ? h : "", h != null && (t != null || o != null) ? " " : "", t != null ? t : "", t != null && o != null ? "-" : "", o != null ? o : "");
83 private static string[] create_data (uint count) {
84 string[] numbers = new string[count];
85 for (uint idx = 0; idx < count; idx++) {
91 string? t = thousands[th];
92 string c = print3digit (n % 1000);
93 num = "%s%s%s%s%s".printf(c, t != null ? " " : "", t != null ? t : "", num != null ? " " : "", num != null ? num : "");
101 numbers[idx] = (owned)num;
103 return (owned)numbers;
106 private static string[] sort_array (owned string[] array) {
107 qsort_with_data<string> (array, sizeof(string), (a, b) => {return strcmp(*(string **)a, *(string **)b);});