File: ReadWriteSuite.cpp

package info (click to toggle)
libspiff 1.0.0-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,240 kB
  • ctags: 1,556
  • sloc: cpp: 10,398; sh: 9,149; makefile: 379; ansic: 83
file content (247 lines) | stat: -rw-r--r-- 6,761 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
/*
 * libSpiff - XSPF playlist handling library
 *
 * Copyright (C) 2006-2008, Sebastian Pipping / Xiph.Org Foundation
 * All rights reserved.
 *
 * 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
 *
 * Sebastian Pipping, sping@xiph.org
 */

#include "ReadWriteSuite.h"
#include <spiff/SpiffProps.h>
#include <spiff/SpiffIndentFormatter.h>
#include <spiff/SpiffReader.h>
#include <spiff/SpiffReaderCallback.h>
#include <spiff/SpiffTrack.h>
#include <spiff/SpiffWriter.h>
#include <cstring>
using namespace Spiff;


ReadWriteSuite::ReadWriteSuite() {
	TEST_ADD(ReadWriteSuite::testRelativeUris);
	TEST_ADD(ReadWriteSuite::testXmlBaseStack);
}


class PropsGrabberCallback : public SpiffReaderCallback {

private:
	SpiffProps * m_props;
	SpiffTrack * m_track;

	void setProps(SpiffProps * props) {
		delete m_props;
		m_props = props;
	}

	void addTrack(SpiffTrack * track) {
		delete m_track;
		m_track = track;
	}

public:
	PropsGrabberCallback() : m_props(NULL), m_track(NULL) { }

	~PropsGrabberCallback() {
		delete m_props;
		m_props = NULL;
		delete m_track;
		m_track = NULL;
	}

	SpiffProps * getProps() const { return m_props; }
	SpiffTrack * getTrack() const { return m_track; }
};


void ReadWriteSuite::testRelativeUris() {
	SpiffProps props;
	SpiffTrack track;

	for (int i = 0; i < 3; i++) {
		// Write to memory
///////////////////////////////////////////////////////////////////////////////
		XML_Char const * const baseUriOne = _PT("http://example.org/one/");
		XML_Char const * const baseUriTwo = _PT("http://example.org/two/");
			
		XML_Char const * const imageUriOne = _PT("http://example.org/one/image.png");
		XML_Char const * const imageUriTwo = _PT("http://example.org/two/image.png");

		props.lendImage(imageUriOne);
		track.lendImage(imageUriOne);
///////////////////////////////////////////////////////////////////////////////

		SpiffIndentFormatter layout;
		SpiffWriter * writer;
		int errorCode;
		switch (i) {
		case 0: // With base URI, embedded
			writer = SpiffWriter::makeWriter(
					layout, baseUriOne,
					SpiffWriter::EMBED_AS_XML_BASE, &errorCode);
			break;
		case 1: // With base URI, _not_ embedded
			writer = SpiffWriter::makeWriter(
					layout,  baseUriOne,
					SpiffWriter::NO_XML_BASE, &errorCode);
			break;
		case 2: // No base URI, all URIs absolute
			writer = SpiffWriter::makeWriter(
					layout, NULL, true, &errorCode);
			break;
		default:
			TEST_ASSERT(false);
		}
		TEST_ASSERT(writer != NULL);
		TEST_ASSERT(errorCode == SPIFF_WRITER_SUCCESS);
		writer->setProps(props);
		writer->addTrack(track);
		char * output = new char[5000];
		int numBytes = 5000;
		int const writeRes = writer->writeMemory(output, numBytes);
		TEST_ASSERT(writeRes == 0);
		TEST_ASSERT(numBytes > 0);
		TEST_ASSERT(output[0] != '\0');

		// Read back from memory
		SpiffReader reader;
		PropsGrabberCallback propsGrabber;
		int const readRes = reader.parseMemory(output, numBytes,
				&propsGrabber, baseUriTwo);
		TEST_ASSERT(readRes == SPIFF_READER_SUCCESS);

		// Compare
		SpiffProps * const afterProps = propsGrabber.getProps();
		TEST_ASSERT(afterProps != NULL);
		SpiffTrack * const afterTrack = propsGrabber.getTrack();
		TEST_ASSERT(afterTrack != NULL);
///////////////////////////////////////////////////////////////////////////////
		XML_Char const * const givenPropsImage = afterProps->getImage();
		XML_Char const * expectedImage = NULL;
		switch (i) {
		case 0: // With base URI, embedded
			expectedImage = imageUriOne;
			break;
		case 1: // With base URI, _not_ embedded
			expectedImage = imageUriTwo;
			break;
		case 2: // No base URI, all URIs absolute
			expectedImage = imageUriOne;
			break;
		default:
			TEST_ASSERT(false);
		}
		TEST_ASSERT(!::PORT_STRCMP(givenPropsImage, expectedImage));

		XML_Char const * const givenTrackImage = afterTrack->getImage();
		XML_Char const * const expectedTrackImage = imageUriTwo;
		TEST_ASSERT(!::PORT_STRCMP(givenTrackImage, expectedImage));
///////////////////////////////////////////////////////////////////////////////

		delete [] output;
		delete writer;
	}
}


class XmlBaseComparatorCallback : public SpiffReaderCallback {

private:
	int i;

public:
	bool success;

private:
	void addTrack(SpiffTrack * track) {
		if (!success) {
			return;
		}

		XML_Char const * given = track->getLocation(0);
		XML_Char const * expected = NULL;
		switch (i) {
		case 0:
			expected = _PT("http://example.org/a/b/c/d/0");
			break;
		case 1:
			expected = _PT("http://example.org/a/b/e/1");
			break;
		case 2:
			expected = _PT("http://example.org/a/b/f/2");
			break;
		case 3:
			expected = _PT("http://example.org/a/b/3");
			break;
		case 4:
			expected = _PT("http://example.org/g/4");
			break;
		case 5:
			expected = _PT("http://example.org/h/i/5");
			break;
		default:
			success = false;
			delete track;
			return;
		}
		i++;

		success = !::PORT_STRCMP(given, expected);
		delete track;
	}

public:
	XmlBaseComparatorCallback() : i(0), success(true) { }
};


void ReadWriteSuite::testXmlBaseStack() {
	char const * const content =
"<?xml version=\"1.0\"?>\n"
"<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xml:base=\"a/\">\n"
"  <trackList xml:base=\"b/\">\n"
"    <track xml:base=\"c/\">\n"
"      <location xml:base=\"d/\">0</location>\n"
"    </track>\n"
"    <track xml:base=\"e/\">\n"
"      <location>1</location>\n"
"    </track>\n"
"    <track>\n"
"      <location xml:base=\"f/\">2</location>\n"
"    </track>\n"
"    <track>\n"
"      <location>3</location>\n"
"    </track>\n"
"    <track xml:base=\"http://example.org/g/\">\n"
"      <location>4</location>\n"
"    </track>\n"
"    <track xml:base=\"http://example.org/h/\">\n"
"      <location xml:base=\"i/\">5</location>\n"
"    </track>\n"
"  </trackList>\n"
"</playlist>\n"
	;

	XmlBaseComparatorCallback callback;
	SpiffReader reader;
	XML_Char const * const baseUri = _PT("http://example.org/");
	int const res = reader.parseMemory(content,
			static_cast<int>(::strlen(content)), &callback, baseUri);
	TEST_ASSERT(res == SPIFF_READER_SUCCESS);
	TEST_ASSERT(callback.success);
}