Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit e72a72d

Browse files
committed
Add a few public examples to object.d
1 parent 64fc5c5 commit e72a72d

File tree

1 file changed

+258
-0
lines changed

1 file changed

+258
-0
lines changed

src/object.d

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,15 @@ void destroy(bool initialize = true, T)(ref T obj) if (is(T == struct))
530530
}
531531
}
532532

533+
@safe unittest
534+
{
535+
struct A { string s = "A"; }
536+
A a = {s: "B"};
537+
assert(a.s == "B");
538+
a.destroy;
539+
assert(a.s == "A");
540+
}
541+
533542
private void _destructRecurse(S)(ref S s)
534543
if (is(S == struct))
535544
{
@@ -1070,6 +1079,90 @@ bool opEquals(const Object lhs, const Object rhs)
10701079
return opEquals(cast()lhs, cast()rhs);
10711080
}
10721081

1082+
/// If aliased to the same object or both null => equal
1083+
@system unittest
1084+
{
1085+
class F { int flag; this(int flag) { this.flag = flag; } }
1086+
1087+
F f;
1088+
assert(f == f); // both null
1089+
f = new F(1);
1090+
assert(f == f); // both aliased to the same object
1091+
}
1092+
1093+
/// If either is null => non-equal
1094+
@system unittest
1095+
{
1096+
class F { int flag; this(int flag) { this.flag = flag; } }
1097+
F f;
1098+
assert(!(new F(0) == f));
1099+
assert(!(f == new F(0)));
1100+
}
1101+
1102+
/// If same exact type => one call to method opEquals
1103+
@system unittest
1104+
{
1105+
class F
1106+
{
1107+
int flag;
1108+
1109+
this(int flag)
1110+
{
1111+
this.flag = flag;
1112+
}
1113+
1114+
override bool opEquals(const Object o)
1115+
{
1116+
return flag == (cast(F) o).flag;
1117+
}
1118+
}
1119+
1120+
F f;
1121+
assert(new F(0) == new F(0));
1122+
assert(!(new F(0) == new F(1)));
1123+
}
1124+
1125+
/// General case => symmetric calls to method opEquals
1126+
@system unittest
1127+
{
1128+
int fEquals, gEquals;
1129+
1130+
class Base
1131+
{
1132+
int flag;
1133+
this(int flag)
1134+
{
1135+
this.flag = flag;
1136+
}
1137+
}
1138+
1139+
class F : Base
1140+
{
1141+
this(int flag) { super(flag); }
1142+
1143+
override bool opEquals(const Object o)
1144+
{
1145+
fEquals++;
1146+
return flag == (cast(Base) o).flag;
1147+
}
1148+
}
1149+
1150+
class G : Base
1151+
{
1152+
this(int flag) { super(flag); }
1153+
1154+
override bool opEquals(const Object o)
1155+
{
1156+
gEquals++;
1157+
return flag == (cast(Base) o).flag;
1158+
}
1159+
}
1160+
1161+
assert(new F(1) == new G(1));
1162+
assert(fEquals == 1);
1163+
assert(gEquals == 1);
1164+
}
1165+
10731166
private extern(C) void _d_setSameMutex(shared Object ownee, shared Object owner) nothrow;
10741167

10751168
void setSameMutex(shared Object ownee, shared Object owner)
@@ -2773,6 +2866,22 @@ class Exception : Throwable
27732866
}
27742867
}
27752868

2869+
///
2870+
@safe unittest
2871+
{
2872+
bool gotCaught;
2873+
try
2874+
{
2875+
throw new Exception("msg");
2876+
}
2877+
catch (Exception e)
2878+
{
2879+
gotCaught = true;
2880+
assert(e.msg == "msg");
2881+
}
2882+
assert(gotCaught);
2883+
}
2884+
27762885
unittest
27772886
{
27782887
{
@@ -2840,6 +2949,22 @@ class Error : Throwable
28402949
Throwable bypassedException;
28412950
}
28422951

2952+
///
2953+
@system unittest
2954+
{
2955+
bool gotCaught;
2956+
try
2957+
{
2958+
throw new Error("msg");
2959+
}
2960+
catch (Error e)
2961+
{
2962+
gotCaught = true;
2963+
assert(e.msg == "msg");
2964+
}
2965+
assert(gotCaught);
2966+
}
2967+
28432968
unittest
28442969
{
28452970
{
@@ -2939,6 +3064,14 @@ void clear(T : Value[Key], Value, Key)(T* aa)
29393064
_aaClear(*cast(void **) aa);
29403065
}
29413066

3067+
///
3068+
@system unittest
3069+
{
3070+
auto aa = ["k1": 2];
3071+
aa.clear;
3072+
assert("k1" !in aa);
3073+
}
3074+
29423075
/***********************************
29433076
* Reorganizes the associative array in place so that lookups are more
29443077
* efficient.
@@ -3022,6 +3155,15 @@ V[K] dup(T : V[K], K, V)(T* aa)
30223155
return (*aa).dup;
30233156
}
30243157

3158+
///
3159+
@safe unittest
3160+
{
3161+
auto aa = ["k1": 2];
3162+
auto a2 = aa.dup;
3163+
aa["k2"] = 3;
3164+
assert("k2" !in a2);
3165+
}
3166+
30253167
// this should never be made public.
30263168
private AARange _aaToRange(T: V[K], K, V)(ref T aa) pure nothrow @nogc @safe
30273169
{
@@ -3068,6 +3210,17 @@ auto byKey(T : V[K], K, V)(T* aa) pure nothrow @nogc
30683210
return (*aa).byKey();
30693211
}
30703212

3213+
///
3214+
@safe unittest
3215+
{
3216+
auto dict = [1: 0, 2: 0];
3217+
int sum;
3218+
foreach (v; dict.byKey)
3219+
sum += v;
3220+
3221+
assert(sum == 3);
3222+
}
3223+
30713224
/***********************************
30723225
* Returns a forward range over the values of the associative array.
30733226
* Params:
@@ -3103,6 +3256,17 @@ auto byValue(T : V[K], K, V)(T* aa) pure nothrow @nogc
31033256
return (*aa).byValue();
31043257
}
31053258

3259+
///
3260+
@safe unittest
3261+
{
3262+
auto dict = ["k1": 1, "k2": 2];
3263+
int sum;
3264+
foreach (v; dict.byValue)
3265+
sum += v;
3266+
3267+
assert(sum == 3);
3268+
}
3269+
31063270
/***********************************
31073271
* Returns a forward range over the key value pairs of the associative array.
31083272
* Params:
@@ -3156,6 +3320,17 @@ auto byKeyValue(T : V[K], K, V)(T* aa) pure nothrow @nogc
31563320
return (*aa).byKeyValue();
31573321
}
31583322

3323+
///
3324+
@safe unittest
3325+
{
3326+
auto dict = ["k1": 1, "k2": 2];
3327+
int sum;
3328+
foreach (e; dict.byKeyValue)
3329+
sum += e.value;
3330+
3331+
assert(sum == 3);
3332+
}
3333+
31593334
/***********************************
31603335
* Returns a dynamic array, the elements of which are the keys in the
31613336
* associative array.
@@ -3178,6 +3353,17 @@ Key[] keys(T : Value[Key], Value, Key)(T *aa) @property
31783353
return (*aa).keys;
31793354
}
31803355

3356+
///
3357+
@system unittest
3358+
{
3359+
auto aa = [1: "v1", 2: "v2"];
3360+
int sum;
3361+
foreach (k; aa.keys)
3362+
sum += k;
3363+
3364+
assert(sum == 3);
3365+
}
3366+
31813367
/***********************************
31823368
* Returns a dynamic array, the elements of which are the values in the
31833369
* associative array.
@@ -3200,6 +3386,17 @@ Value[] values(T : Value[Key], Value, Key)(T *aa) @property
32003386
return (*aa).values;
32013387
}
32023388

3389+
///
3390+
@system unittest
3391+
{
3392+
auto aa = ["k1": 1, "k2": 2];
3393+
int sum;
3394+
foreach (e; aa.values)
3395+
sum += e;
3396+
3397+
assert(sum == 3);
3398+
}
3399+
32033400
/***********************************
32043401
* Looks up key; if it exists returns corresponding value else evaluates and
32053402
* returns defaultValue.
@@ -3222,6 +3419,13 @@ inout(V) get(K, V)(inout(V[K])* aa, K key, lazy inout(V) defaultValue)
32223419
return (*aa).get(key, defaultValue);
32233420
}
32243421

3422+
@safe unittest
3423+
{
3424+
auto aa = ["k1": 1];
3425+
assert(aa.get("k1", 0) == 1);
3426+
assert(aa.get("k2", 0) == 0);
3427+
}
3428+
32253429
/***********************************
32263430
* Looks up key; if it exists returns corresponding value else evaluates
32273431
* value, adds it to the associative array and returns it.
@@ -3250,6 +3454,15 @@ ref V require(K, V)(ref V[K] aa, K key, lazy V value = V.init)
32503454
return found ? *p : (*p = value);
32513455
}
32523456

3457+
///
3458+
@safe unittest
3459+
{
3460+
auto aa = ["k1": 1];
3461+
assert(aa.require("k1", 0) == 1);
3462+
assert(aa.require("k2", 0) == 0);
3463+
assert(aa["k2"] == 0);
3464+
}
3465+
32533466
// Constraints for aa update. Delegates, Functions or Functors (classes that
32543467
// provide opCall) are allowed. See unittest for an example.
32553468
private
@@ -3309,6 +3522,26 @@ if (isCreateOperation!(C, V) && isUpdateOperation!(U, V))
33093522
*p = update(*p);
33103523
}
33113524

3525+
///
3526+
@system unittest
3527+
{
3528+
auto aa = ["k1": 1];
3529+
3530+
aa.update("k1", {
3531+
return -1; // create (won't be executed
3532+
}, (ref int v) {
3533+
return v + 1; // update
3534+
});
3535+
assert(aa["k1"] == 2);
3536+
3537+
aa.update("k2", {
3538+
return 0; // create
3539+
}, (ref int v) {
3540+
return -1; // update (won't be executed)
3541+
});
3542+
assert(aa["k2"] == 0);
3543+
}
3544+
33123545
unittest
33133546
{
33143547
static struct S
@@ -3868,6 +4101,12 @@ version (D_Ddoc)
38684101
static import core.internal.hash;
38694102
return core.internal.hash.hashOf(arg);
38704103
}
4104+
4105+
@safe unittest
4106+
{
4107+
auto h1 = "my.string".hashOf;
4108+
assert(h1 == "my.string".hashOf);
4109+
}
38714110
}
38724111
else
38734112
{
@@ -4173,6 +4412,16 @@ private size_t getArrayHash(in TypeInfo element, in void* ptr, in size_t count)
41734412
return _dup!(T, Unconst!T)(a);
41744413
}
41754414

4415+
///
4416+
@safe unittest
4417+
{
4418+
auto arr = [1, 2];
4419+
auto arr2 = arr.dup;
4420+
arr[0] = 0;
4421+
assert(arr == [0, 2]);
4422+
assert(arr2 == [1, 2]);
4423+
}
4424+
41764425
/// ditto
41774426
// const overload to support implicit conversion to immutable (unique result, see DIP29)
41784427
@property T[] dup(T)(const(T)[] a)
@@ -4205,6 +4454,15 @@ private size_t getArrayHash(in TypeInfo element, in void* ptr, in size_t count)
42054454
return a.dup;
42064455
}
42074456

4457+
///
4458+
@safe unittest
4459+
{
4460+
char[] arr = ['a', 'b', 'c'];
4461+
string s = arr.idup;
4462+
arr[0] = '.';
4463+
assert(s == "abc");
4464+
}
4465+
42084466
private U[] _trustedDup(T, U)(T[] a) @trusted
42094467
{
42104468
return _dup!(T, U)(a);

0 commit comments

Comments
 (0)