File: ccx_encoders_splitbysentence_suite.c

package info (click to toggle)
ccextractor 0.87+ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 10,064 kB
  • sloc: ansic: 172,772; makefile: 777; sh: 622; python: 319
file content (428 lines) | stat: -rw-r--r-- 11,356 bytes parent folder | download
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <check.h>
#include "ccx_encoders_splitbysentence_suite.h"

// -------------------------------------
// MOCKS
// -------------------------------------
typedef int64_t LLONG;
#include "../src/lib_ccx/ccx_encoders_common.h"

//#define ENABLE_OCR


// -------------------------------------
// Private SBS-functions (for testing only)
// -------------------------------------
void sbs_reset_context();
struct cc_subtitle * sbs_append_string(unsigned char * str, LLONG time_from, LLONG time_trim, void * sbs_context);

// -------------------------------------
// Helpers
// -------------------------------------

struct cc_subtitle * helper_sbs_append_sub_from_file(FILE * fd, struct encoder_ctx * context) {
	// TODO : I am not sure about correctness of this line,
	// but I just want to test the code:
	char localbuf[2000];
	char * str;
	LLONG time_from, time_trim;

	if (feof(fd)) {
		return NULL;
	}

	if ( 0 >= fscanf(fd, "%ld %ld", &time_from, &time_trim)) {
		return NULL;
	}

	fgets(localbuf, 2000, fd);

	// skip leading spaces
	str = localbuf;
	for (; isspace(*str); str++){
	}

	// replace LITERAL "\n" with a newline char
	size_t i, j, L;
	L = strlen(str);
	for (i=0, j=0; i < L; i++, j++) {

		if (i + 1 < L) {
			if (str[i] == '\\' && str[i+1] == 'n') {
				i++;
				str[i] = '\n';
			}
		}
		str[j] = str[i];
	}

	// remove trailing newline:
	for(; j>0 && str[j] == '\n'; j--) {
		str[j] = 0;
	}
	//str1 = strdup(str);
	struct cc_subtitle * sub;
	sub = sbs_append_string(str, time_from, time_trim, sbs_init_context());
	//free(str1);
	return sub;
}

struct cc_subtitle * helper_create_sub(char * str, LLONG time_from, LLONG time_trim) {
	struct cc_bitmap* rect;
	struct cc_subtitle * sub = (struct cc_subtitle *)malloc(sizeof(struct cc_subtitle));
	sub->type = CC_BITMAP;
	sub->start_time = 1;
	sub->end_time = 100;

	rect = malloc(sizeof(struct cc_bitmap));
	rect->data[0] = strdup(str);
	rect->data[1] = NULL;

	sub->data = rect;
	sub->nb_data = 1;

	return sub;
}

// -------------------------------------
// MOCKS
// -------------------------------------
struct encoder_ctx * context;

unsigned char * paraof_ocrtext(void * sub) {
	// this is OCR -> text converter.
	// now, in our test cases, we will pass TEXT instead of OCR.
	// and will return passed text as result
	struct cc_bitmap* rect;

	rect = ((struct cc_subtitle *)sub)->data;
#ifdef ENABLE_OCR
	return strdup(rect->data[0]);
#else
	return NULL;
#endif

}

// -------------------------------------
// TEST preparations
// -------------------------------------
void setup(void)
{
	context = (struct encoder_ctx *)malloc(sizeof(struct encoder_ctx));
	sbs_reset_context();
}

void teardown(void)
{
	free(context);
}

// -------------------------------------
// TESTS
// -------------------------------------
START_TEST(test_sbs_one_simple_sentence)
{
	printf(
"=====================\n\
test_sbs_one_simple_sentence\n\
=====================\n"
	);

	struct cc_subtitle * sub = helper_create_sub("Simple sentence.", 1, 100);
	struct cc_subtitle * out = reformat_cc_bitmap_through_sentence_buffer(sub, context);

	ck_assert_ptr_ne(out, NULL);
	ck_assert_str_eq(out->data, "Simple sentence.");
	ck_assert_ptr_eq(out->next, NULL);
	ck_assert_ptr_eq(out->prev, NULL);
}
END_TEST


START_TEST(test_sbs_two_sentences_with_rep)
{
	printf(
"=====================\n\
test_sbs_two_sentences_with_rep\n\
=====================\n"
	);
	struct cc_subtitle * sub1 = helper_create_sub("asdf", 1, 100);
	struct cc_subtitle * out1 = reformat_cc_bitmap_through_sentence_buffer(sub1, context);
	ck_assert_ptr_eq(out1, NULL);

	// second sub:
	struct cc_subtitle * sub2 = helper_create_sub("asdf Hello.", 101, 200);
	struct cc_subtitle * out2 = reformat_cc_bitmap_through_sentence_buffer(sub2, context);

	ck_assert_ptr_ne(out2, NULL);
	ck_assert_str_eq(out2->data, "asdf Hello.");
	ck_assert_ptr_eq(out2->next, NULL);
	ck_assert_ptr_eq(out2->prev, NULL);}
END_TEST


START_TEST(test_sbs_append_string_two_separate)
{
	printf(
"=====================\n\
test_sbs_append_string_two_separate\n\
=====================\n"
	);
	unsigned char * test_strings[] = {
		"First string.",
		"Second string."
	};
	struct cc_subtitle * sub;
	unsigned char * str;

	// first string
	str = strdup(test_strings[0]);
	sub = NULL;
	sub = sbs_append_string(str, 1, 20, sbs_init_context());
	ck_assert_ptr_ne(sub, NULL);
	ck_assert_str_eq(sub->data, test_strings[0]);
	ck_assert_int_eq(sub->start_time, 1);
	ck_assert_int_eq(sub->end_time, 20);

	// second string:
	str = strdup(test_strings[1]);
	sub = NULL;
	sub = sbs_append_string(str, 21, 40, sbs_init_context());

	ck_assert_ptr_ne(sub, NULL);
	ck_assert_str_eq(sub->data, test_strings[1]);
	ck_assert_int_eq(sub->start_time, 21);
	ck_assert_int_eq(sub->end_time, 40);
}
END_TEST

START_TEST(test_sbs_append_string_two_with_broken_sentence)
{
	char * test_strings[] = {
		"First string",
		" ends here, deabbea."
	};
	struct cc_subtitle * sub;
	char * str;

	// first string
	str = strdup(test_strings[0]);
	sub = sbs_append_string(str, 1, 3, sbs_init_context());

	ck_assert_ptr_eq(sub, NULL);

	// second string:
	str = strdup(test_strings[1]);
	sub = sbs_append_string(str, 4, 5, sbs_init_context());

	ck_assert_ptr_ne(sub, NULL);
	ck_assert_str_eq(sub->data, "First string ends here, deabbea.");
	ck_assert_int_eq(sub->start_time, 1);
	ck_assert_int_eq(sub->end_time, 5);
}
END_TEST

START_TEST(test_sbs_append_string_two_intersecting)
{
	char * test_strings[] = {
		"First string",
		"First string ends here."
	};
	struct cc_subtitle * sub;
	char * str;

	// first string
	str = strdup(test_strings[0]);
	sub = sbs_append_string(str, 1, 20, sbs_init_context());

	ck_assert_ptr_eq(sub, NULL);
	free(sub);

	// second string:
	str = strdup(test_strings[1]);
	sub = sbs_append_string(str, 21, 40, sbs_init_context());

	ck_assert_ptr_ne(sub, NULL);
	ck_assert_str_eq(sub->data, "First string ends here.");
	ck_assert_int_eq(sub->start_time, 1);
	ck_assert_int_eq(sub->end_time, 40);
}
END_TEST



START_TEST(test_sbs_append_string_00)
{
	FILE * fsample;
	int skip;

	fsample = fopen("samples/sbs_append_string_00", "r");
	struct cc_subtitle * sub;

	skip = 2;
	while (skip-- > 0) {
		sub = helper_sbs_append_sub_from_file(fsample, context);
		ck_assert_ptr_eq(sub, NULL);
	}

	sub = helper_sbs_append_sub_from_file(fsample, context);
	ck_assert_ptr_ne(sub, NULL);
	ck_assert_str_eq(sub->data, "in all these different environments, just \
doing what turkeys want to do‘ Let's get the sport now with an update from \
Sky Sports News HQ.");
	ck_assert_int_eq(sub->start_time, 1);
	ck_assert_int_eq(sub->end_time, 3467);

	sub = helper_sbs_append_sub_from_file(fsample, context);
	ck_assert_ptr_eq(sub, NULL);

	sub = helper_sbs_append_sub_from_file(fsample, context);
	ck_assert_ptr_ne(sub, NULL);
	ck_assert_str_eq(sub->data, "Malky Mackay believes he deserves a second \
chance after being appointed the Scottish FA's new Performance Director.");
	ck_assert_int_eq(sub->start_time, 3468);
	ck_assert_int_eq(sub->end_time, 16361);

	skip = 10;
	while (skip-- > 0) {
		sub = helper_sbs_append_sub_from_file(fsample, context);
		ck_assert_ptr_eq(sub, NULL);
	}

	sub = helper_sbs_append_sub_from_file(fsample, context);
	ck_assert_ptr_ne(sub, NULL);
// TODO : It is too hard to fix this error automatically (hard for me)
// May be someone knows, how to implement this checker, and then next
// assertion could be uncommented
// maxkoryukov/ccextractor#3
	/*
	ck_assert_str_eq(sub->data, "Mackay was sacked by Cardiff in 2013 after it \
emerged he sent racist There has been some opposition When I said at the time, \
I deeply to his appointment but he's asked regret.");
	*/
	ck_assert_int_eq(sub->start_time, 16362);
	ck_assert_int_eq(sub->end_time, 38924);


	skip = 19;
	while (skip-- > 0) {

// if (sub != NULL) {
// 	printf("%d :> [%s]\n", skip, sub->data);
// }
		sub = helper_sbs_append_sub_from_file(fsample, context);
		// TODO : this subs should give an empty response
		// But the algorithm is not smart enough
		// maxkoryukov/ccextractor#3
		/*
		ck_assert_ptr_eq(sub, NULL);
		*/
	}

	sub = helper_sbs_append_sub_from_file(fsample, context);
	ck_assert_ptr_ne(sub, NULL);
// TODO : It is too hard to fix this error automatically (hard for me)
// May be someone knows, how to implement this checker, and then next
// assertion could be uncommented
// maxkoryukov/ccextractor#3
	/*
	ck_assert_str_eq(sub->data, "It was said in I am in support to \
shoot support to shoot —— I spoke to the two individuals that were \
involved."
	);
	ck_assert_int_eq(sub->start_time, 38925);
	*/
	ck_assert_int_eq(sub->end_time, 47406);

	fclose(fsample);
}
END_TEST


START_TEST(test_sbs_append_string_01)
{
	FILE * fsample;
	int skip;

	fsample = fopen("samples/sbs_append_string_01", "r");
	struct cc_subtitle * sub;

	sub = helper_sbs_append_sub_from_file(fsample, context);
	ck_assert_ptr_eq(sub, NULL);

	// 2
	sub = helper_sbs_append_sub_from_file(fsample, context);
	ck_assert_ptr_ne(sub, NULL);
	ck_assert_str_eq(sub->data, "Oleon costs.");

	// 3
	sub = helper_sbs_append_sub_from_file(fsample, context);
	ck_assert_ptr_ne(sub, NULL);
	ck_assert_str_eq(sub->data, "buried in the annex, 95 Oleon costs.");
	ck_assert_int_eq(sub->start_time, 190);    // = <sub start>
	ck_assert_int_eq(sub->end_time, 783);      // = <sub start>  +  <available time,889-190=699 > * <sentence alphanum, 28>  /  <sub alphanum, 33>
	ck_assert_ptr_eq(sub->next, NULL);

	skip = 5;
	while (skip--) {
		sub = helper_sbs_append_sub_from_file(fsample, context);
		ck_assert_ptr_eq(sub, NULL);
	}

	// 13
	sub = helper_sbs_append_sub_from_file(fsample, context);
	ck_assert_ptr_ne(sub, NULL);
	ck_assert_str_eq(sub->data, "Didn't want to acknowledge the pressures on hospitals, schools and infrastructure.");
	ck_assert_int_eq(sub->start_time, 784);
	ck_assert_int_eq(sub->end_time, 5159);
	ck_assert_ptr_eq(sub->next, NULL);

	skip = 20;
	while (skip--) {
		sub = helper_sbs_append_sub_from_file(fsample, context);
		ck_assert_ptr_eq(sub, NULL);
	}

	sub = helper_sbs_append_sub_from_file(fsample, context);
	ck_assert_ptr_ne(sub, NULL);
	ck_assert_int_eq(sub->start_time, 5160);
	ck_assert_int_eq(sub->end_time, 16100);
	ck_assert_ptr_eq(sub->next, NULL);
	ck_assert_str_eq(sub->data, "If we go to the Australian size system, we can have the migrants who will contribute and not drain the economy.");

	fclose(fsample);
}
END_TEST


Suite * ccx_encoders_splitbysentence_suite(void)
{
	Suite *s;
	TCase *tc_core;

	s = suite_create("Sentence Buffer");

	/* Overall tests */
	tc_core = tcase_create("SB: Overall: ");

	tcase_add_checked_fixture(tc_core, setup, teardown);
	tcase_add_test(tc_core, test_sbs_one_simple_sentence);
	tcase_add_test(tc_core, test_sbs_two_sentences_with_rep);
	suite_add_tcase(s, tc_core);

	/**/
	TCase *tc_append_string;
	tc_append_string = tcase_create("SB: append_string: ");
	tcase_add_checked_fixture(tc_append_string, setup, teardown);

	tcase_add_test(tc_append_string, test_sbs_append_string_two_separate);
	tcase_add_test(tc_append_string, test_sbs_append_string_two_with_broken_sentence);
	tcase_add_test(tc_append_string, test_sbs_append_string_two_intersecting);
	tcase_add_test(tc_append_string, test_sbs_append_string_00);
	tcase_add_test(tc_append_string, test_sbs_append_string_01);

	suite_add_tcase(s, tc_append_string);

	return s;
}