Debian Codegraph @

libphobos/libdruntime/rt/adi.d

raw
1 /**
2 * Implementation of dynamic array property support routines.
3 *
4 * Copyright: Copyright Digital Mars 2000 - 2015.
5 * License: Distributed under the
6 * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7 * (See accompanying file LICENSE)
8 * Authors: Walter Bright
9 * Source: $(DRUNTIMESRC rt/_adi.d)
10 */
11
12 module rt.adi;
13
14 // debug = adi; // uncomment to turn on debugging printf's
15
16 debug (adi) import core.stdc.stdio : printf;
17
18 /***************************************
19 * Support for array equality test.
20 * Returns:
21 * 1 equal
22 * 0 not equal
23 */
24
25 extern (C) int _adEq2(void[] a1, void[] a2, TypeInfo ti)
26 {
27 debug(adi) printf("_adEq2(a1.length = %zd, a2.length = %zd)\n", a1.length, a2.length);
28 if (a1.length != a2.length)
29 return 0; // not equal
30 if (!ti.equals(&a1, &a2))
31 return 0;
32 return 1;
33 }
34
35 @safe unittest
36 {
37 debug(adi) printf("array.Eq unittest\n");
38
39 struct S(T) { T val; }
40 alias String = S!string;
41 alias Float = S!float;
42
43 String[1] a = [String("hello"c)];
44
45 assert(a != [String("hel")]);
46 assert(a != [String("helloo")]);
47 assert(a != [String("betty")]);
48 assert(a == [String("hello")]);
49 assert(a != [String("hxxxx")]);
50
51 Float[1] fa = [Float(float.nan)];
52 assert(fa != fa);
53 }
54
55 unittest
56 {
57 debug(adi) printf("struct.Eq unittest\n");
58
59 static struct TestStruct
60 {
61 int value;
62
63 bool opEquals(const TestStruct rhs) const
64 {
65 return value == rhs.value;
66 }
67 }
68
69 TestStruct[] b = [TestStruct(5)];
70 TestStruct[] c = [TestStruct(6)];
71 assert(_adEq2(*cast(void[]*)&b, *cast(void[]*)&c, typeid(TestStruct[])) == false);
72 assert(_adEq2(*cast(void[]*)&b, *cast(void[]*)&b, typeid(TestStruct[])) == true);
73 }