Files
sdk/runtime/tests/vm/dart/string_equals_test.dart
T
Robert Nystrom 11b81715ad Reformat runtime/ using the 3.8 formatter style.
Change-Id: I7b5e5dd768c87f28848ee02050582d23f3604cb1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426286
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
2025-05-03 12:54:15 -07:00

53 lines
1.6 KiB
Dart

// Copyright (c) 2022, 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.
//
// Verifies that string equal implementation correctly handles strings of
// various lengths.
import 'dart:math';
import "package:expect/expect.dart";
compare(List<int> ints, String s_piece, String t_piece, bool expectedEquality) {
final s = String.fromCharCodes(ints);
String s_mid = s + s_piece + s;
String t_mid = s + t_piece + s;
Expect.isFalse(identical(s_mid, t_mid));
Expect.equals(s_mid == t_mid, expectedEquality);
String s_tail = s + s_piece;
String t_tail = s + t_piece;
Expect.isFalse(identical(s_tail, t_tail));
Expect.equals(s_tail == t_tail, expectedEquality);
String s_head = s_piece + s;
String t_head = t_piece + s;
Expect.isFalse(identical(s_head, t_head));
Expect.equals(s_head == t_head, expectedEquality);
}
main() {
const int maxStringLength = 128;
// OneByteString
for (int i = 0; i < maxStringLength; i++) {
final l = List.generate(i, (n) => (Random().nextInt(30) + 40));
compare(l, 'a', 'b', false);
compare(l, 'a', 'a', true);
}
// TwoByteString
for (int i = 0; i < maxStringLength; i++) {
final l = List.generate(i, (n) => (Random().nextInt(1024) + 1024));
compare(
l,
String.fromCharCodes(<int>[1042]),
String.fromCharCodes(<int>[1043]),
false,
);
compare(
l,
String.fromCharCodes(<int>[1042]),
String.fromCharCodes(<int>[1042]),
true,
);
}
}