1+ namespace DotNext . Runtime ;
2+
3+ [ TestClass ]
4+ public sealed class BoxedValueTests
5+ {
6+ [ TestMethod ]
7+ public void BoxUnbox ( )
8+ {
9+ var obj = ( BoxedValue < int > ) 42 ;
10+ Assert . AreEqual ( 42 . GetHashCode ( ) , obj . GetHashCode ( ) ) ;
11+ Assert . AreEqual ( 42 , obj . Value ) ;
12+ Assert . AreEqual ( 42 , ( int ) obj ) ;
13+ Assert . AreEqual ( typeof ( int ) , obj . GetType ( ) ) ;
14+ }
15+
16+ [ TestMethod ]
17+ public void Unwrap ( )
18+ {
19+ object ? obj = null ;
20+ Assert . IsNull ( BoxedValue < int > . GetTypedReference ( obj ) ) ;
21+
22+ obj = 42 ;
23+ Assert . AreEqual ( 42 , BoxedValue < int > . GetTypedReference ( obj ) . Value ) ;
24+
25+ obj = string . Empty ;
26+ Assert . ThrowsException < ArgumentException > ( ( ) => BoxedValue < int > . GetTypedReference ( obj ) ) ;
27+ }
28+
29+ [ TestMethod ]
30+ public void ToUntypedReference ( )
31+ {
32+ ValueType obj = BoxedValue < int > . Box ( 42 ) ;
33+ Assert . AreEqual ( 42 , obj ) ;
34+ }
35+
36+ private struct MutableStruct
37+ {
38+ public int Value ;
39+ }
40+
41+ [ TestMethod ]
42+ public void BitwiseCopyImmutable ( )
43+ {
44+ var boxed1 = ( BoxedValue < int > ) 42 ;
45+ var boxed2 = boxed1 . Copy ( ) ;
46+ Assert . AreNotSame ( boxed1 , boxed2 ) ;
47+ Assert . AreEqual ( 42 , boxed1 ) ;
48+ Assert . AreEqual ( 42 , boxed2 ) ;
49+ }
50+
51+ [ TestMethod ]
52+ public void BitwiseCopyMutable ( )
53+ {
54+ var boxed1 = ( BoxedValue < MutableStruct > ) new MutableStruct ( ) ;
55+ var boxed2 = boxed1 . Copy ( ) ;
56+ Assert . AreNotSame ( boxed1 , boxed2 ) ;
57+
58+ boxed1 . Value . Value = 42 ;
59+ boxed2 . Value . Value = 43 ;
60+
61+ Assert . AreNotEqual ( boxed1 . Value . Value , boxed2 . Value . Value ) ;
62+ }
63+ }
0 commit comments