<commit_before>//===--- RealTestSupport.swift --------------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import XCTest
import RealModule

#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
typealias TestLiteralType = Float80
#else
typealias TestLiteralType = Double
#endif

@discardableResult
func assertClose<T>(
  _ expected: TestLiteralType,
  _ observed: T,
  allowedError: T = 16,
  file: StaticString = #file,
  line: UInt = #line
) -> T where T: BinaryFloatingPoint {
  // Shortcut relative-error check if we got the sign wrong; it's OK to
  // underflow to zero, but we do not want to allow going right through
  // zero and getting the sign wrong.
  guard observed.sign == expected.sign else {
    print("Sign was wrong: expected \(expected) but saw \(observed).")
    XCTFail(file: file, line: line)
    return .infinity
  }
  if observed.isNaN && expected.isNaN { return 0 }
  // If T(expected) is zero or infinite, and matches observed, the error
  // is zero.
  let expectedT = T(expected)
  if observed.isZero && expectedT.isZero { return 0 }
  if observed.isInfinite && expectedT.isInfinite { return 0 }
  // Special-case where only one of expectedT or observed is infinity.
  // Artificially knock everything down a binade, treat actual infinity as
  // the base of the next binade up.
  func topBinade(signOf x: T) -> T {
    T(signOf: x, magnitudeOf: T.greatestFiniteMagnitude.binade)
  }
  if observed.isInfinite {
    return assertClose(
      expected/2, topBinade(signOf: observed),
      allowedError: allowedError, file: file, line: line
    )
  }
  if expectedT.isInfinite {
    return assertClose(
      TestLiteralType(topBinade(signOf: expectedT)), observed/2,
      allowedError: allowedError, file: file, line: line
    )
  }
  // Compute error in ulp, compare to tolerance.
  let absoluteError = (TestLiteralType(observed) - expected).magnitude
  let scale = max(expectedT.magnitude, T.leastNormalMagnitude).ulp
  let ulps = T(absoluteError/TestLiteralType(scale))
  if ulps > allowedError {
    print("ULP error was unacceptably large: expected \(expected) but saw \(observed) (\(ulps)-ulp error).")
    XCTFail(file: file, line: line)
  }
  return ulps
}

func assertClose<T>(
  _ expected: TestLiteralType,
  _ observed: T,
  allowedError: T = 16,
  worstError: inout T,
  file: StaticString = #file,
  line: UInt = #line
) where T: BinaryFloatingPoint {
  worstError = max(worstError, assertClose(
    expected, observed, allowedError: allowedError, file: file, line: line
  ))
}<commit_msg>file -> filePath, too.<commit_after>//===--- RealTestSupport.swift --------------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import XCTest
import RealModule

#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
typealias TestLiteralType = Float80
#else
typealias TestLiteralType = Double
#endif

@discardableResult
func assertClose<T>(
  _ expected: TestLiteralType,
  _ observed: T,
  allowedError: T = 16,
  file: StaticString = #filePath,
  line: UInt = #line
) -> T where T: BinaryFloatingPoint {
  // Shortcut relative-error check if we got the sign wrong; it's OK to
  // underflow to zero, but we do not want to allow going right through
  // zero and getting the sign wrong.
  guard observed.sign == expected.sign else {
    print("Sign was wrong: expected \(expected) but saw \(observed).")
    XCTFail(file: file, line: line)
    return .infinity
  }
  if observed.isNaN && expected.isNaN { return 0 }
  // If T(expected) is zero or infinite, and matches observed, the error
  // is zero.
  let expectedT = T(expected)
  if observed.isZero && expectedT.isZero { return 0 }
  if observed.isInfinite && expectedT.isInfinite { return 0 }
  // Special-case where only one of expectedT or observed is infinity.
  // Artificially knock everything down a binade, treat actual infinity as
  // the base of the next binade up.
  func topBinade(signOf x: T) -> T {
    T(signOf: x, magnitudeOf: T.greatestFiniteMagnitude.binade)
  }
  if observed.isInfinite {
    return assertClose(
      expected/2, topBinade(signOf: observed),
      allowedError: allowedError, file: file, line: line
    )
  }
  if expectedT.isInfinite {
    return assertClose(
      TestLiteralType(topBinade(signOf: expectedT)), observed/2,
      allowedError: allowedError, file: file, line: line
    )
  }
  // Compute error in ulp, compare to tolerance.
  let absoluteError = (TestLiteralType(observed) - expected).magnitude
  let scale = max(expectedT.magnitude, T.leastNormalMagnitude).ulp
  let ulps = T(absoluteError/TestLiteralType(scale))
  if ulps > allowedError {
    print("ULP error was unacceptably large: expected \(expected) but saw \(observed) (\(ulps)-ulp error).")
    XCTFail(file: file, line: line)
  }
  return ulps
}

func assertClose<T>(
  _ expected: TestLiteralType,
  _ observed: T,
  allowedError: T = 16,
  worstError: inout T,
  file: StaticString = #filePath,
  line: UInt = #line
) where T: BinaryFloatingPoint {
  worstError = max(worstError, assertClose(
    expected, observed, allowedError: allowedError, file: file, line: line
  ))
}
{'fraction_non_alphanumeric': 0.1438650834623989, 'fraction_numerical': 0.008260196179659268, 'mean_word_length': 3.0757363253856944, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '5984920', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1724, 'n_words': 582}