xref: /haiku/src/tests/kits/media/AreaTest.cpp (revision 46d4471af7fad4e52cfbd09174598cf5318aceed)
1 /*
2  * Copyright 2014 Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "AreaTest.h"
8 
9 #include <OS.h>
10 
11 #include <cppunit/TestCaller.h>
12 #include <cppunit/TestSuite.h>
13 
14 
15 AreaTest::AreaTest()
16 {
17 }
18 
19 
20 AreaTest::~AreaTest()
21 {
22 }
23 
24 
25 void
26 AreaTest::TestAreas()
27 {
28 	int * ptr = new int[1];
29 	char *adr;
30 	area_id id;
31 	ptrdiff_t offset;
32 
33 	area_info info;
34 	id = area_for(ptr);
35 	get_area_info(id, &info);
36 	adr = (char *)info.address;
37 	offset = (ptrdiff_t)ptr - (ptrdiff_t)adr;
38 
39 
40 	char * adrclone1;
41 	char * adrclone2;
42 	int * ptrclone1;
43 	int * ptrclone2;
44 	area_id idclone1;
45 	area_id idclone2;
46 
47 	idclone1 = clone_area("clone 1", (void **)&adrclone1, B_ANY_ADDRESS,
48 		B_READ_AREA | B_WRITE_AREA, id);
49 	idclone2 = clone_area("clone 2", (void **)&adrclone2, B_ANY_ADDRESS,
50 		B_READ_AREA | B_WRITE_AREA, id);
51 
52 	ptrclone1 = (int *)(adrclone1 + offset);
53 	ptrclone2 = (int *)(adrclone2 + offset);
54 
55 	// Check that he pointer is inside the area returned by area_for...
56 	CPPUNIT_ASSERT(offset >= 0);
57 
58 	// Chech that the clones have different IDs
59 	CPPUNIT_ASSERT(id != idclone1);
60 	CPPUNIT_ASSERT(id != idclone2);
61 	CPPUNIT_ASSERT(idclone1 != idclone2);
62 
63 	// Check that the clones have different addresses
64 	CPPUNIT_ASSERT(adr != adrclone1);
65 	CPPUNIT_ASSERT(adr != adrclone2);
66 	CPPUNIT_ASSERT(adrclone1 != adrclone2);
67 
68 	// Check that changes on one view of the area are visible in others.
69 	ptr[0] = 0x12345678;
70 	CPPUNIT_ASSERT(ptr[0] == ptrclone1[0]);
71 	CPPUNIT_ASSERT(ptrclone2[0] == ptrclone1[0]);
72 	CPPUNIT_ASSERT_EQUAL(0x12345678, ptrclone2[0]);
73 }
74 
75 
76 /*static*/ void
77 AreaTest::AddTests(BTestSuite& parent)
78 {
79 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("AreaTest");
80 
81 	suite.addTest(new CppUnit::TestCaller<AreaTest>(
82 		"AreaTest::TestAreas", &AreaTest::TestAreas));
83 
84 	parent.addTest("AreaTest", &suite);
85 }
86