Files
sdk/tests/language_2/const/map_test.dart
T
Robert Nystrom fc1b1ecc71 Move files under language_2 into subdirectories.
Change-Id: Idbcc965a27e9ffeedf5e0a1068b019de4193070f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127745
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
2019-12-11 19:18:00 +00:00

39 lines
1.5 KiB
Dart

// Copyright (c) 2014, 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.
getValueNonOptimized(x) {
// DateTime.now() cannot be predicted statically, never equal to ASCII 42 '*'.
if (new DateTime.now() == 42) return getValueNonOptimized(2);
return x;
}
main() {
// TODO(terry): Should check:
// - const map is immutable
// - simple expressions are const e.g., 2 + 3, true && !false, etc.
// - const with final and/or static with same const attributes
// Additionally new class instances with a static const same identity
// - const for all types (int, num, double, String, boolean, and objects)
// - canonicalization - const created only once same identity e.g.,
//
// getConstMap() => const [1, 2];
// var a = getConstMap();
// var b = getConstMap();
// Expect.equals(a.hashCode, b.hashCode);
// Make sure that const maps use the == operator and not object identity. The
// specification does not explicitly require it, otherwise ints and Strings
// wouldn't make much sense as keys.
var m = const {1: 42, "foo": 499, 2: "bar"};
Expect.equals(42, m[getValueNonOptimized(1.0)]);
Expect.equals(
499, m[getValueNonOptimized(new String.fromCharCodes("foo".runes))]);
Expect.equals('bar', m[getValueNonOptimized(2)]);
}