Add allocator analysis test for the J model

Change-Id: I42fc8bfa4ea35c9025f6f48faa73a7cc16aab3b8
Reviewed-on: https://dart-review.googlesource.com/c/92287
Reviewed-by: Stephen Adams <sra@google.com>
diff --git a/tests/compiler/dart2js/allocator_analysis/jallocator_analysis_test.dart b/tests/compiler/dart2js/allocator_analysis/jallocator_analysis_test.dart
new file mode 100644
index 0000000..c8c401d
--- /dev/null
+++ b/tests/compiler/dart2js/allocator_analysis/jallocator_analysis_test.dart
@@ -0,0 +1,55 @@
+// Copyright (c) 2019, 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 'dart:io';
+import 'package:async_helper/async_helper.dart';
+import 'package:compiler/src/compiler.dart';
+import 'package:compiler/src/constants/values.dart';
+import 'package:compiler/src/elements/entities.dart';
+import 'package:compiler/src/ir/util.dart';
+import 'package:compiler/src/js_backend/allocator_analysis.dart';
+import 'package:compiler/src/js_model/js_world.dart';
+import 'package:compiler/src/util/features.dart';
+import 'package:kernel/ast.dart' as ir;
+import '../equivalence/id_equivalence.dart';
+import '../equivalence/id_equivalence_helper.dart';
+
+main(List<String> args) {
+  asyncTest(() async {
+    Directory dataDir = new Directory.fromUri(Platform.script.resolve('jdata'));
+    await checkTests(dataDir, const JAllocatorAnalysisDataComputer(),
+        args: args, testOmit: false);
+  });
+}
+
+class Tags {
+  static const String initialValue = 'initial';
+}
+
+class JAllocatorAnalysisDataComputer extends DataComputer<Features> {
+  const JAllocatorAnalysisDataComputer();
+
+  @override
+  void computeMemberData(Compiler compiler, MemberEntity member,
+      Map<Id, ActualData<Features>> actualMap,
+      {bool verbose: false}) {
+    if (member.isField && member.isInstanceMember) {
+      JsClosedWorld closedWorld = compiler.backendClosedWorldForTesting;
+      JAllocatorAnalysis allocatorAnalysis = closedWorld.allocatorAnalysis;
+      ir.Member node = closedWorld.elementMap.getMemberDefinition(member).node;
+      ConstantValue initialValue = allocatorAnalysis.initializerValue(member);
+      Features features = new Features();
+      if (initialValue != null) {
+        features[Tags.initialValue] = initialValue.toStructuredText();
+      }
+      Id id = computeEntityId(node);
+      actualMap[id] = new ActualData<Features>(
+          id, features, computeSourceSpanFromTreeNode(node), member);
+    }
+  }
+
+  @override
+  DataInterpreter<Features> get dataValidator =>
+      const FeaturesDataInterpreter();
+}
diff --git a/tests/compiler/dart2js/allocator_analysis/jdata/simple_initializers.dart b/tests/compiler/dart2js/allocator_analysis/jdata/simple_initializers.dart
new file mode 100644
index 0000000..be12d6c
--- /dev/null
+++ b/tests/compiler/dart2js/allocator_analysis/jdata/simple_initializers.dart
@@ -0,0 +1,110 @@
+// Copyright (c) 2019, 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.
+
+main() {
+  new Class1();
+  new Class2();
+}
+
+const bool const1 = true;
+
+class Class1 {
+  /*element: Class1.field0:initial=NullConstant*/
+  var field0;
+
+  /*element: Class1.field1:initial=NullConstant*/
+  var field1 = null;
+
+  /*element: Class1.field2:initial=BoolConstant(true)*/
+  var field2 = true;
+
+  /*element: Class1.field3:initial=BoolConstant(false)*/
+  var field3 = false;
+
+  /*element: Class1.field4:initial=IntConstant(0)*/
+  var field4 = 0;
+
+  /*element: Class1.field5:initial=IntConstant(1)*/
+  var field5 = 1;
+
+  /*element: Class1.field6:initial=StringConstant("")*/
+  var field6 = '';
+
+  /*element: Class1.field7:initial=StringConstant("foo")*/
+  var field7 = 'foo';
+
+  /*element: Class1.field8:*/
+  var field8 = 0.5;
+
+  /*element: Class1.field9:*/
+  var field9 = const [];
+
+  /*element: Class1.field10:*/
+  var field10 = const {};
+
+  /*element: Class1.field11:*/
+  var field11 = #foo;
+
+  /*element: Class1.field12:*/
+  var field12 = 2 + 3;
+
+  /*element: Class1.field13:*/
+  var field13 = const1;
+}
+
+class Class2 {
+  /*element: Class2.field1:*/
+  var field1;
+
+  /*element: Class2.field2:*/
+  var field2;
+
+  /*element: Class2.field3:*/
+  var field3;
+
+  /*element: Class2.field4:*/
+  var field4;
+
+  /*element: Class2.field5:*/
+  var field5;
+
+  /*element: Class2.field6:*/
+  var field6;
+
+  /*element: Class2.field7:*/
+  var field7;
+
+  /*element: Class2.field8:*/
+  var field8;
+
+  /*element: Class2.field9:*/
+  var field9;
+
+  /*element: Class2.field10:*/
+  var field10;
+
+  /*element: Class2.field11:*/
+  var field11;
+
+  /*element: Class2.field12:*/
+  var field12;
+
+  /*element: Class2.field13:*/
+  var field13;
+
+  Class2()
+      : field1 = null,
+        field2 = true,
+        field3 = false,
+        field4 = 0,
+        field5 = 1,
+        field6 = '',
+        field7 = 'foo',
+        field8 = 0.5,
+        field9 = const [],
+        field10 = const {},
+        field11 = #foo,
+        field12 = 2 + 3,
+        field13 = const1;
+}
diff --git a/tests/compiler/dart2js/allocator_analysis/kallocator_analysis_test.dart b/tests/compiler/dart2js/allocator_analysis/kallocator_analysis_test.dart
index a4b8e88..2ffab6d 100644
--- a/tests/compiler/dart2js/allocator_analysis/kallocator_analysis_test.dart
+++ b/tests/compiler/dart2js/allocator_analysis/kallocator_analysis_test.dart
@@ -34,7 +34,7 @@
   void computeMemberData(Compiler compiler, MemberEntity member,
       Map<Id, ActualData<Features>> actualMap,
       {bool verbose: false}) {
-    if (member.isField) {
+    if (member.isField && member.isInstanceMember) {
       KernelFrontEndStrategy frontendStrategy = compiler.frontendStrategy;
       KAllocatorAnalysis allocatorAnalysis =
           compiler.backend.allocatorResolutionAnalysisForTesting;