Spaces:
Runtime error
Runtime error
File size: 4,714 Bytes
28958dc |
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 |
#include <unittest/unittest.h>
#include <thrust/device_vector.h>
#include <thrust/device_reference.h>
void TestDeviceReferenceConstructorFromDeviceReference(void)
{
typedef int T;
thrust::device_vector<T> v(1,0);
thrust::device_reference<T> ref = v[0];
// ref equals the object at v[0]
ASSERT_EQUAL(v[0], ref);
// the address of ref equals the address of v[0]
ASSERT_EQUAL(&v[0], &ref);
// modifying v[0] modifies ref
v[0] = 13;
ASSERT_EQUAL(13, ref);
ASSERT_EQUAL(v[0], ref);
// modifying ref modifies v[0]
ref = 7;
ASSERT_EQUAL(7, v[0]);
ASSERT_EQUAL(v[0], ref);
}
DECLARE_UNITTEST(TestDeviceReferenceConstructorFromDeviceReference);
void TestDeviceReferenceConstructorFromDevicePointer(void)
{
typedef int T;
thrust::device_vector<T> v(1,0);
thrust::device_ptr<T> ptr = &v[0];
thrust::device_reference<T> ref(ptr);
// ref equals the object pointed to by ptr
ASSERT_EQUAL(*ptr, ref);
// the address of ref equals ptr
ASSERT_EQUAL(ptr, &ref);
// modifying *ptr modifies ref
*ptr = 13;
ASSERT_EQUAL(13, ref);
ASSERT_EQUAL(v[0], ref);
// modifying ref modifies *ptr
ref = 7;
ASSERT_EQUAL(7, *ptr);
ASSERT_EQUAL(v[0], ref);
}
DECLARE_UNITTEST(TestDeviceReferenceConstructorFromDevicePointer);
void TestDeviceReferenceAssignmentFromDeviceReference(void)
{
// test same types
typedef int T0;
thrust::device_vector<T0> v0(2,0);
thrust::device_reference<T0> ref0 = v0[0];
thrust::device_reference<T0> ref1 = v0[1];
ref0 = 13;
ref1 = ref0;
// ref1 equals 13
ASSERT_EQUAL(13, ref1);
ASSERT_EQUAL(ref0, ref1);
// test different types
typedef float T1;
thrust::device_vector<T1> v1(1,0.0f);
thrust::device_reference<T1> ref2 = v1[0];
ref2 = ref1;
// ref2 equals 13.0f
ASSERT_EQUAL(13.0f, ref2);
ASSERT_EQUAL(ref0, ref2);
ASSERT_EQUAL(ref1, ref2);
}
DECLARE_UNITTEST(TestDeviceReferenceAssignmentFromDeviceReference);
void TestDeviceReferenceManipulation(void)
{
typedef int T1;
thrust::device_vector<T1> v(1,0);
thrust::device_ptr<T1> ptr = &v[0];
thrust::device_reference<T1> ref(ptr);
// reset
ref = 0;
// test prefix increment
++ref;
ASSERT_EQUAL(1, ref);
ASSERT_EQUAL(1, *ptr);
ASSERT_EQUAL(1, v[0]);
// reset
ref = 0;
// test postfix increment
T1 x1 = ref++;
ASSERT_EQUAL(0, x1);
ASSERT_EQUAL(1, ref);
ASSERT_EQUAL(1, *ptr);
ASSERT_EQUAL(1, v[0]);
// reset
ref = 0;
// test addition-assignment
ref += 5;
ASSERT_EQUAL(5, ref);
ASSERT_EQUAL(5, *ptr);
ASSERT_EQUAL(5, v[0]);
// reset
ref = 0;
// test prefix decrement
--ref;
ASSERT_EQUAL(-1, ref);
ASSERT_EQUAL(-1, *ptr);
ASSERT_EQUAL(-1, v[0]);
// reset
ref = 0;
// test subtraction-assignment
ref -= 5;
ASSERT_EQUAL(-5, ref);
ASSERT_EQUAL(-5, *ptr);
ASSERT_EQUAL(-5, v[0]);
// reset
ref = 1;
// test multiply-assignment
ref *= 5;
ASSERT_EQUAL(5, ref);
ASSERT_EQUAL(5, *ptr);
ASSERT_EQUAL(5, v[0]);
// reset
ref = 5;
// test divide-assignment
ref /= 5;
ASSERT_EQUAL(1, ref);
ASSERT_EQUAL(1, *ptr);
ASSERT_EQUAL(1, v[0]);
// reset
ref = 5;
// test modulus-assignment
ref %= 5;
ASSERT_EQUAL(0, ref);
ASSERT_EQUAL(0, *ptr);
ASSERT_EQUAL(0, v[0]);
// reset
ref = 1;
// test left shift-assignment
ref <<= 1;
ASSERT_EQUAL(2, ref);
ASSERT_EQUAL(2, *ptr);
ASSERT_EQUAL(2, v[0]);
// reset
ref = 2;
// test right shift-assignment
ref >>= 1;
ASSERT_EQUAL(1, ref);
ASSERT_EQUAL(1, *ptr);
ASSERT_EQUAL(1, v[0]);
// reset
ref = 0;
// test OR-assignment
ref |= 1;
ASSERT_EQUAL(1, ref);
ASSERT_EQUAL(1, *ptr);
ASSERT_EQUAL(1, v[0]);
// reset
ref = 1;
// test XOR-assignment
ref ^= 1;
ASSERT_EQUAL(0, ref);
ASSERT_EQUAL(0, *ptr);
ASSERT_EQUAL(0, v[0]);
// test equality of const references
thrust::device_reference<const T1> ref1 = v[0];
ASSERT_EQUAL(true, ref1 == ref);
}
DECLARE_UNITTEST(TestDeviceReferenceManipulation);
void TestDeviceReferenceSwap(void)
{
typedef int T;
thrust::device_vector<T> v(2);
thrust::device_reference<T> ref1 = v.front();
thrust::device_reference<T> ref2 = v.back();
ref1 = 7;
ref2 = 13;
// test thrust::swap()
thrust::swap(ref1, ref2);
ASSERT_EQUAL(13, ref1);
ASSERT_EQUAL(7, ref2);
// test .swap()
ref1.swap(ref2);
ASSERT_EQUAL(7, ref1);
ASSERT_EQUAL(13, ref2);
}
DECLARE_UNITTEST(TestDeviceReferenceSwap);
|