@@ -530,6 +530,15 @@ void destroy(bool initialize = true, T)(ref T obj) if (is(T == struct))
530
530
}
531
531
}
532
532
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
+
533
542
private void _destructRecurse (S)(ref S s)
534
543
if (is (S == struct ))
535
544
{
@@ -1070,6 +1079,90 @@ bool opEquals(const Object lhs, const Object rhs)
1070
1079
return opEquals (cast ()lhs, cast ()rhs);
1071
1080
}
1072
1081
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
+
1073
1166
private extern (C) void _d_setSameMutex(shared Object ownee, shared Object owner) nothrow ;
1074
1167
1075
1168
void setSameMutex (shared Object ownee, shared Object owner)
@@ -2773,6 +2866,22 @@ class Exception : Throwable
2773
2866
}
2774
2867
}
2775
2868
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
+
2776
2885
unittest
2777
2886
{
2778
2887
{
@@ -2840,6 +2949,22 @@ class Error : Throwable
2840
2949
Throwable bypassedException;
2841
2950
}
2842
2951
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
+
2843
2968
unittest
2844
2969
{
2845
2970
{
@@ -2939,6 +3064,14 @@ void clear(T : Value[Key], Value, Key)(T* aa)
2939
3064
_aaClear(* cast (void ** ) aa);
2940
3065
}
2941
3066
3067
+ // /
3068
+ @system unittest
3069
+ {
3070
+ auto aa = [" k1" : 2 ];
3071
+ aa.clear;
3072
+ assert (" k1" ! in aa);
3073
+ }
3074
+
2942
3075
/* **********************************
2943
3076
* Reorganizes the associative array in place so that lookups are more
2944
3077
* efficient.
@@ -3022,6 +3155,15 @@ V[K] dup(T : V[K], K, V)(T* aa)
3022
3155
return (* aa).dup ;
3023
3156
}
3024
3157
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
+
3025
3167
// this should never be made public.
3026
3168
private AARange _aaToRange (T: V[K], K, V)(ref T aa) pure nothrow @nogc @safe
3027
3169
{
@@ -3068,6 +3210,17 @@ auto byKey(T : V[K], K, V)(T* aa) pure nothrow @nogc
3068
3210
return (* aa).byKey ();
3069
3211
}
3070
3212
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
+
3071
3224
/* **********************************
3072
3225
* Returns a forward range over the values of the associative array.
3073
3226
* Params:
@@ -3103,6 +3256,17 @@ auto byValue(T : V[K], K, V)(T* aa) pure nothrow @nogc
3103
3256
return (* aa).byValue ();
3104
3257
}
3105
3258
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
+
3106
3270
/* **********************************
3107
3271
* Returns a forward range over the key value pairs of the associative array.
3108
3272
* Params:
@@ -3156,6 +3320,17 @@ auto byKeyValue(T : V[K], K, V)(T* aa) pure nothrow @nogc
3156
3320
return (* aa).byKeyValue ();
3157
3321
}
3158
3322
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
+
3159
3334
/* **********************************
3160
3335
* Returns a dynamic array, the elements of which are the keys in the
3161
3336
* associative array.
@@ -3178,6 +3353,17 @@ Key[] keys(T : Value[Key], Value, Key)(T *aa) @property
3178
3353
return (* aa).keys ;
3179
3354
}
3180
3355
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
+
3181
3367
/* **********************************
3182
3368
* Returns a dynamic array, the elements of which are the values in the
3183
3369
* associative array.
@@ -3200,6 +3386,17 @@ Value[] values(T : Value[Key], Value, Key)(T *aa) @property
3200
3386
return (* aa).values ;
3201
3387
}
3202
3388
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
+
3203
3400
/* **********************************
3204
3401
* Looks up key; if it exists returns corresponding value else evaluates and
3205
3402
* returns defaultValue.
@@ -3222,6 +3419,13 @@ inout(V) get(K, V)(inout(V[K])* aa, K key, lazy inout(V) defaultValue)
3222
3419
return (* aa).get (key, defaultValue);
3223
3420
}
3224
3421
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
+
3225
3429
/* **********************************
3226
3430
* Looks up key; if it exists returns corresponding value else evaluates
3227
3431
* 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)
3250
3454
return found ? * p : (* p = value);
3251
3455
}
3252
3456
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
+
3253
3466
// Constraints for aa update. Delegates, Functions or Functors (classes that
3254
3467
// provide opCall) are allowed. See unittest for an example.
3255
3468
private
@@ -3309,6 +3522,26 @@ if (isCreateOperation!(C, V) && isUpdateOperation!(U, V))
3309
3522
* p = update(* p);
3310
3523
}
3311
3524
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
+
3312
3545
unittest
3313
3546
{
3314
3547
static struct S
@@ -3868,6 +4101,12 @@ version (D_Ddoc)
3868
4101
static import core.internal.hash ;
3869
4102
return core.internal.hash.hashOf (arg);
3870
4103
}
4104
+
4105
+ @safe unittest
4106
+ {
4107
+ auto h1 = " my.string" .hashOf;
4108
+ assert (h1 == " my.string" .hashOf);
4109
+ }
3871
4110
}
3872
4111
else
3873
4112
{
@@ -4173,6 +4412,16 @@ private size_t getArrayHash(in TypeInfo element, in void* ptr, in size_t count)
4173
4412
return _dup! (T, Unconst! T)(a);
4174
4413
}
4175
4414
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
+
4176
4425
// / ditto
4177
4426
// const overload to support implicit conversion to immutable (unique result, see DIP29)
4178
4427
@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)
4205
4454
return a.dup ;
4206
4455
}
4207
4456
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
+
4208
4466
private U[] _trustedDup (T, U)(T[] a) @trusted
4209
4467
{
4210
4468
return _dup! (T, U)(a);
0 commit comments