dart-sdk/tests/language/unsorted/savannah_test.dart
Robert Nystrom d4154ac6dc Migrate language_2/unsorted to NNBD.
Change-Id: Iad2963c7f9c184b089dc6d15aa4442f58d194bc2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151983
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
2020-06-26 18:34:14 +00:00

76 lines
2 KiB
Dart

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test using an identity hash.
import "package:expect/expect.dart";
abstract class BigGame {
String get name;
}
// Giraffe overrides hashCode and provides its own identity hash.
class Giraffe implements BigGame {
final String name;
final int identityHash_;
Giraffe(this.name) : identityHash_ = nextId() {}
int get hashCode {
return identityHash_;
}
// Calculate identity hash for a giraffe.
static int nextId_ = 17;
static int nextId() {
return nextId_++;
}
}
// Zebra relies on the system provided identity hash.
class Zebra implements BigGame {
final String name;
Zebra(this.name) {}
}
class SavannahTest {
static void testMain() {
Map<BigGame, String> savannah = new Map<BigGame, String>();
Giraffe giraffe1 = new Giraffe("Tony");
Giraffe giraffe2 = new Giraffe("Rose");
savannah[giraffe1] = giraffe1.name;
savannah[giraffe2] = giraffe2.name;
print("giraffe1 hash: ${giraffe1.hashCode}");
print("giraffe2 hash: ${giraffe2.hashCode}");
var count = savannah.length;
print("getCount is $count");
Expect.equals(2, count);
print("giraffe1: ${savannah[giraffe1]}");
print("giraffe2: ${savannah[giraffe2]}");
Expect.equals("Tony", savannah[giraffe1]);
Expect.equals("Rose", savannah[giraffe2]);
Zebra zebra1 = new Zebra("Paolo");
Zebra zebra2 = new Zebra("Zeeta");
savannah[zebra1] = zebra1.name;
savannah[zebra2] = zebra2.name;
print("zebra1 hash: ${zebra1.hashCode}");
print("zebra2 hash: ${zebra2.hashCode}");
count = savannah.length;
print("getCount is $count");
Expect.equals(4, count);
print("zebra1: ${savannah[zebra1]}");
print("zebra2: ${savannah[zebra2]}");
Expect.equals("Paolo", savannah[zebra1]);
Expect.equals("Zeeta", savannah[zebra2]);
}
}
main() {
SavannahTest.testMain();
}