Files
sdk/tests/language/const/map_hashcode_override_test.dart
T
Aske Simon Christensen de3cec3a47 Make hashcode override test throw instead of looping indefinitely.
This achieves the same thing (test that `hashCode` is not called), but
avoids a costly timeout on failure. This drastically cuts down the total
testing time for the language and corelib suites for dart2wasm (which
intends not to implement a special case here, since it is going to be
illegal in the future anyway).

Change-Id: I040925ebd8271a5816c81bc16484c0a96626857d
Cq-Include-Trybots: luci.dart.try:vm-kernel-precomp-linux-debug-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/244061
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
2022-05-11 11:48:26 +00:00

42 lines
1.3 KiB
Dart

// Copyright (c) 2021, 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.
import 'package:expect/expect.dart';
/// Returns its argument.
///
/// Prevents static optimizations and inlining.
@pragma('vm:never-inline')
@pragma('dart2js:noInline')
dynamic getValueNonOptimized(dynamic x) {
// DateTime.now() cannot be predicted statically, never equal to 42.
if (DateTime.now().millisecondsSinceEpoch == 42) {
return getValueNonOptimized(2);
}
return x;
}
class Nasty {
final int n;
const Nasty(this.n);
int get hashCode {
throw "Evil hashCode";
}
}
main() {
// Test that when using an instance of a custom class as a key in a const Map,
// its hash code is computed as its identity hash, and even if the class
// overrides `hashCode`, that `hashCode` implementation is not called.
final map = const {1: 42, 'foo': 499, 2: 'bar', Nasty(1): 'baz'};
Expect.equals(42, map[getValueNonOptimized(1.0)]);
Expect.equals(
499, map[getValueNonOptimized(new String.fromCharCodes('foo'.runes))]);
Expect.equals('bar', map[getValueNonOptimized(2)]);
Expect.isNull(map[getValueNonOptimized(Nasty(1))]);
Expect.equals('baz', map[getValueNonOptimized(const Nasty(1))]);
}