<commit_before>// Copyright (c) 2016, 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.
// VMOptions=--enable_asserts

import "package:expect/expect.dart";

bool testAssociativity(Function f) {
  // Example from https://en.wikipedia.org/wiki/Floating_point
  // Test that (a + b) + c == a + (b + c).
  double a = f(1234.567);  // Chop literals.
  double b = f(45.67834);
  double c = f(0.0004);
  double x = (a + b) + c;  // Chop result of multiplication or division only.
  double y = a + (b + c);
  print("x: $x");
  print("y: $y");
  return x == y;
}

bool testDistributivity(Function f) {
  // Example from https://en.wikipedia.org/wiki/Floating_point
  // Test that (a + b)*c == a*c + b*c.
  double a = f(1234.567);  // Chop literals.
  double b = f(1.234567);
  double c = f(3.333333);
  double x = f((a + b)*c);  // Chop result of multiplication.
  double y = f(a*c) + f(b*c);
  print("x: $x");
  print("y: $y");
  return x == y;
}

// Simulate precision checking with assert.
assertP(double d) {
  assert(d == d.p);
}

main() {
  // The getter p keeps only 20 (by default) bits after the decimal point.
  Expect.equals(0.0, 0.0.p);  // 0.0 has no 1-bit after the decimal point.
  Expect.equals(1.5, 1.5.p);  // 1.5 has a single 1-bit after the decimal point.
  Expect.notEquals(1.1, 1.1.p);  // 1.1 has many 1-bits after the decimal point.
  Expect.notEquals(1/3, (1/3).p);  // 0.33333333... ditto.

  Expect.equals(1.1 + 1/3, 1/3 + 1.1);  // Test addition commutativity.
  Expect.equals(1.1.p + (1/3).p, (1/3).p + 1.1.p);
  Expect.equals(1.1 * 1/3, 1/3 * 1.1);  // Test multiplication commutativity.
  Expect.equals(1.1.p * (1/3).p, (1/3).p * 1.1.p);

  print("Without chopping fractional bits:");
  Expect.isFalse(testAssociativity((x) => x));
  Expect.isFalse(testDistributivity((x) => x));
  print("With chopping fractional bits:");
  Expect.isTrue(testAssociativity((x) => x.p));
  Expect.isTrue(testDistributivity((x) => x.p));

  // Check that p works with NaN and Infinity.
  Expect.isTrue(double.NAN.p.isNaN);
  Expect.isTrue(double.INFINITY.p.isInfinite);
  Expect.isFalse(double.INFINITY.p.isNegative);
  Expect.isTrue(double.NEGATIVE_INFINITY.p.isInfinite);
  Expect.isTrue(double.NEGATIVE_INFINITY.p.isNegative);

  // Check use of assert to verify precision.
  assertP(1.5);
  assertP(1.1.p);
  Expect.throws(() => assertP(1.1), (e) => e is AssertionError);
  assertP(1.23456789.p);
  Expect.throws(() => assertP(1.23456789), (e) => e is AssertionError);
}<commit_msg>Update new test to check asserts only if they are enabled and remove VM option. It seems like asserts are not supported correctly in product mode.<commit_after>// Copyright (c) 2016, 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";

bool testAssociativity(Function f) {
  // Example from https://en.wikipedia.org/wiki/Floating_point
  // Test that (a + b) + c == a + (b + c).
  double a = f(1234.567);  // Chop literals.
  double b = f(45.67834);
  double c = f(0.0004);
  double x = (a + b) + c;  // Chop result of multiplication or division only.
  double y = a + (b + c);
  print("x: $x");
  print("y: $y");
  return x == y;
}

bool testDistributivity(Function f) {
  // Example from https://en.wikipedia.org/wiki/Floating_point
  // Test that (a + b)*c == a*c + b*c.
  double a = f(1234.567);  // Chop literals.
  double b = f(1.234567);
  double c = f(3.333333);
  double x = f((a + b)*c);  // Chop result of multiplication.
  double y = f(a*c) + f(b*c);
  print("x: $x");
  print("y: $y");
  return x == y;
}

// Simulate precision checking with assert.
assertP(double d) {
  assert(d == d.p);
}

bool assertionsEnabled() {
  try {
    assert(false);
    return false;
  } on AssertionError catch (e) {
    return true;
  }
  return false;
}

main() {
  // The getter p keeps only 20 (by default) bits after the decimal point.
  Expect.equals(0.0, 0.0.p);  // 0.0 has no 1-bit after the decimal point.
  Expect.equals(1.5, 1.5.p);  // 1.5 has a single 1-bit after the decimal point.
  Expect.notEquals(1.1, 1.1.p);  // 1.1 has many 1-bits after the decimal point.
  Expect.notEquals(1/3, (1/3).p);  // 0.33333333... ditto.

  Expect.equals(1.1 + 1/3, 1/3 + 1.1);  // Test addition commutativity.
  Expect.equals(1.1.p + (1/3).p, (1/3).p + 1.1.p);
  Expect.equals(1.1 * 1/3, 1/3 * 1.1);  // Test multiplication commutativity.
  Expect.equals(1.1.p * (1/3).p, (1/3).p * 1.1.p);

  print("Without chopping fractional bits:");
  Expect.isFalse(testAssociativity((x) => x));
  Expect.isFalse(testDistributivity((x) => x));
  print("With chopping fractional bits:");
  Expect.isTrue(testAssociativity((x) => x.p));
  Expect.isTrue(testDistributivity((x) => x.p));

  // Check that p works with NaN and Infinity.
  Expect.isTrue(double.NAN.p.isNaN);
  Expect.isTrue(double.INFINITY.p.isInfinite);
  Expect.isFalse(double.INFINITY.p.isNegative);
  Expect.isTrue(double.NEGATIVE_INFINITY.p.isInfinite);
  Expect.isTrue(double.NEGATIVE_INFINITY.p.isNegative);

  // Check use of assert to verify precision.
  if (assertionsEnabled()) {
    assertP(1.5);
    assertP(1.1.p);
    Expect.throws(() => assertP(1.1), (e) => e is AssertionError);
    assertP(1.23456789.p);
    Expect.throws(() => assertP(1.23456789), (e) => e is AssertionError);
  }
}
{'fraction_non_alphanumeric': 0.17218423893486864, 'fraction_numerical': 0.04893846707448723, 'mean_word_length': 3.1702925731432856, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4548838', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2075, 'n_words': 564}