File: test_read.c

package info (click to toggle)
libdiscid 0.6.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,816 kB
  • sloc: sh: 4,660; ansic: 2,553; makefile: 85
file content (151 lines) | stat: -rw-r--r-- 4,255 bytes parent folder | download | duplicates (5)
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
/* --------------------------------------------------------------------------

   MusicBrainz -- The Internet music metadatabase

   Copyright (C) 2013 Johannes Dewender

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

--------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <discid/discid.h>
#include "test.h"


int main(int argc, char *argv[]) {
	DiscId *d;
	DiscId *d2;
	int i, first, last;
	int subtest_passed;
	int offset, previous_offset;
	char *error_msg;
	int feature_read;
	int sectors;
	int *track_offsets;
	char *device;

	if (argc > 1) {
		device = argv[1];
	} else {
		device = NULL;
	}

	d = discid_new();

	announce("discid_has_feature");
	feature_read = discid_has_feature(DISCID_FEATURE_READ);
	evaluate(feature_read == 0 || feature_read == 1);

	announce("discid_read_sparse");
	if (!discid_read_sparse(d, device, 0)) {
		printf("SKIP\n");

		announce("discid_get_error_msg");
		error_msg = discid_get_error_msg(d);
		evaluate(strlen(error_msg) > 0);

		printf("\t%s\n\n", error_msg);
		discid_free(d);
		return 77; /* code for SKIP in autotools */
	}
	/* we shouldn't be here if the feature is not implemented */
	evaluate(discid_has_feature(DISCID_FEATURE_READ));

	announce("discid_get_default_device");
	/* In contrast to test_core, there should be a device now.  */
	evaluate(strlen(discid_get_default_device()) > 0);

	announce("discid_get_id");
	evaluate(equal_int(strlen(discid_get_id(d)), 28));

	announce("discid_get_freedb_id");
	evaluate(equal_int(strlen(discid_get_freedb_id(d)), 8));

	announce("discid_get_toc_string");
	evaluate(strlen(discid_get_toc_string(d)) > 0);

	announce("discid_get_submission_url");
	evaluate(strlen(discid_get_submission_url(d)) > 0);

	announce("discid_get_first_track_num");
	first = discid_get_first_track_num(d);
	evaluate(first > 0);
	announce("discid_get_last_track_num");
	last = discid_get_last_track_num(d);
	evaluate(last > 0);

	announce("discid_get_sectors");
	sectors = discid_get_sectors(d);
	evaluate(equal_int(sectors, discid_get_track_offset(d, last)
				+ discid_get_track_length(d, last)));

	announce("discid_get_track_offset sane");
	previous_offset = 0;
	subtest_passed = 0;
	for (i=first; i<=last; i++) {
		offset = discid_get_track_offset(d, i);
		if (offset <= sectors) {
			subtest_passed++;
		}
		if (previous_offset) {
			if (offset >= previous_offset) {
				subtest_passed++;
			}
		}
		previous_offset = offset;
	}
	evaluate(equal_int(subtest_passed, 2 * (last - first + 1) - 1));

	announce("discid_get_mcn empty");
	evaluate(strlen(discid_get_mcn(d)) == 0);

	announce("discid_get_track_isrc empty");
	subtest_passed = 0;
	for (i=first; i<=last; i++) {
		if (strlen(discid_get_track_isrc(d, i)) == 0) {
			subtest_passed++;
		}
	}
	evaluate(equal_int(subtest_passed, last - first + 1));

	announce("read/put idempotence");
	d2 = discid_new();
	/* create track offset array */
	track_offsets = malloc(sizeof (int) * (last - first + 2));
	memset(track_offsets, 0, sizeof (int) * (last - first + 2));
	track_offsets[0] = sectors;
	for (i=first; i<=last; i++) {
		track_offsets[i] = discid_get_track_offset(d, i);
	}
	discid_put(d2, first, last, track_offsets);
	evaluate(equal_str(discid_get_id(d2), discid_get_id(d))
			&& equal_str(discid_get_submission_url(d2),
				discid_get_submission_url(d)));
	free(track_offsets);
	discid_free(d2);

	announce("discid_get_error_msg");
	evaluate(strlen(discid_get_error_msg(d)) == 0);


	discid_free(d);

	return !test_result();
}

/* EOF */