5252class Test_Map extends Test_ {
5353
5454 public static void main (String [] args ) {
55-
56- testSets ();
55+
56+ System .out .println ("Equivalence tests asserted" );
57+ assert (new Integer (3 ) == 3 );
58+ assert (new Integer (3 ) != new Integer (3 ));
59+
60+ String a = "x" ;
61+ assert ("xxx" == "xx" + "x" );
62+ assert ("xxx" == ("xx" + a ).intern ());
63+
64+ try {
65+ assert ("xxx" != "xx" + a );
66+ } catch (AssertionError e ) {
67+ System .out .println ("Known assertion error for a=\" x\" ;\" xxx\" != \" xx\" + a " );
68+ }
69+ try {
70+ assert (new String ("xxx" ).toString () != ("xx" + a ).intern ());
71+ } catch (AssertionError e ) {
72+ System .out .println ("Known assertion error for a=\" x\" ; new String(\" xxx\" ).toString() != (\" xx\" + a).intern()" );
73+ }
74+ assert (new String ("xxx" ).intern () == "xxx" );
75+ assert (!(new String ("xxx" ) == "xxx" ));
76+ assert (new String ("xxx" ) != "xxx" );
77+ assert (new String ("xxx" ) != new String ("xxx" ));
78+ assert (new String ("xxx" ) != new String ("xxx" ).intern ());
79+
80+
81+
82+ System .out .println ("The identityHashCode for these three are all different in Java, but not in Java:" );
83+ System .out .println (System .identityHashCode ("test" ));
84+ String s = "" ;
85+ System .out .println (System .identityHashCode ("test" + s ));
86+ s = "st" ;
87+ System .out .println (System .identityHashCode ("te" + s ));
88+ System .out .println (System .identityHashCode ("te" + s ));
89+
90+ System .out .println ("The identityHashCode for an interned string is the same as its literal" );
91+ System .out .println (System .identityHashCode (("te" + s ).intern ()));
92+
93+ System .out .println ("The identityHashCode for these three are all different in Java and JavaScript:" );
94+ System .out .println (System .identityHashCode (new String ("test" )));
95+ System .out .println (System .identityHashCode (new String ("test" )));
96+ System .out .println (System .identityHashCode (new String ("test" )));
97+ testSets ();
5798
5899 testIdentity ();
59100
@@ -75,18 +116,25 @@ public boolean equals(Object o) {
75116
76117 private static void testSets () {
77118 Set <Object > hs = new HashSet <>();
119+ System .out .println ("HashSet should have 6 members:" );
78120 hs .add (null );
79121 hs .add (new int [] {1 ,2 ,3 });
80122 hs .add (new int [] {1 ,2 ,3 });
81123 hs .add (new int [] {1 ,2 ,3 });
82124 hs .add (Integer .valueOf (3 )); // identical
83125 hs .add (Integer .valueOf (3 )); // identical
84126 hs .add (Integer .valueOf (3 )); // identical
127+
128+ hs .add (new String ("testing" )); // different
129+ String ing = "ing" ;
130+ hs .add ("test" + ing ); // different from above
131+ hs .add ("testing" ); // different from above
132+
85133 for (Object o : hs ) {
86134 System .out .println (o );
87135 }
88136 System .out .println (hs .size ());
89- assert (hs .size () == 5 );
137+ assert (hs .size () == 6 );
90138
91139 Iterator <Object > it ;
92140 it = hs .iterator ();
@@ -114,9 +162,13 @@ private static void testIdentity() {
114162 IdentityHashMap <Object , Object > hmi = new IdentityHashMap <>();
115163
116164 hmi .put (new String ("testing" ), "testing1" ); // different
165+
117166
118- hmi .put ("testing" , "testing2" ); // different from above
119- hmi .put ("testing" , "testing3" ); // same as above
167+ // String ing = "ing";
168+ // hmi.put("test" + ing, "testing2"); // different from above
169+
170+ hmi .put ("testing" , "testing3" ); // different from above
171+ hmi .put ("testing" , "testing4" ); // same as above
120172
121173
122174 try {
@@ -164,9 +216,12 @@ private static void testIdentity() {
164216 System .out .println ("HashMap should have 9 members:" );
165217 HashMap <Object , Object > hm = new HashMap <>();
166218
167- hm .put ("testing" , "testing2" ); // identical
168- hm .put ("testing" , "testing3" ); // identical
169- hm .put (new String ("testing" ), "testing1" ); // identical
219+ hm .put (new String ("testing" ), "testing1" ); // same
220+ hm .put ("testing" , "testing2" ); // same
221+ String ing = "ing" ;
222+ hmi .put ("test" + ing , "testing3" ); // same
223+ hm .put ("testing" , "testing4" ); // same
224+
170225
171226 try {
172227 hm .put (new Integer (null ), "126new" ); // unique
0 commit comments