Blame nss/external_tests/google_test/gtest/samples/sample1.cc

Packit 40b132
// Copyright 2005, Google Inc.
Packit 40b132
// All rights reserved.
Packit 40b132
//
Packit 40b132
// Redistribution and use in source and binary forms, with or without
Packit 40b132
// modification, are permitted provided that the following conditions are
Packit 40b132
// met:
Packit 40b132
//
Packit 40b132
//     * Redistributions of source code must retain the above copyright
Packit 40b132
// notice, this list of conditions and the following disclaimer.
Packit 40b132
//     * Redistributions in binary form must reproduce the above
Packit 40b132
// copyright notice, this list of conditions and the following disclaimer
Packit 40b132
// in the documentation and/or other materials provided with the
Packit 40b132
// distribution.
Packit 40b132
//     * Neither the name of Google Inc. nor the names of its
Packit 40b132
// contributors may be used to endorse or promote products derived from
Packit 40b132
// this software without specific prior written permission.
Packit 40b132
//
Packit 40b132
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 40b132
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 40b132
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 40b132
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 40b132
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 40b132
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 40b132
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 40b132
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 40b132
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 40b132
Packit 40b132
// A sample program demonstrating using Google C++ testing framework.
Packit 40b132
//
Packit 40b132
// Author: wan@google.com (Zhanyong Wan)
Packit 40b132
Packit 40b132
#include "sample1.h"
Packit 40b132
Packit 40b132
// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
Packit 40b132
int Factorial(int n) {
Packit 40b132
  int result = 1;
Packit 40b132
  for (int i = 1; i <= n; i++) {
Packit 40b132
    result *= i;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  return result;
Packit 40b132
}
Packit 40b132
Packit 40b132
// Returns true iff n is a prime number.
Packit 40b132
bool IsPrime(int n) {
Packit 40b132
  // Trivial case 1: small numbers
Packit 40b132
  if (n <= 1) return false;
Packit 40b132
Packit 40b132
  // Trivial case 2: even numbers
Packit 40b132
  if (n % 2 == 0) return n == 2;
Packit 40b132
Packit 40b132
  // Now, we have that n is odd and n >= 3.
Packit 40b132
Packit 40b132
  // Try to divide n by every odd number i, starting from 3
Packit 40b132
  for (int i = 3; ; i += 2) {
Packit 40b132
    // We only have to try i up to the squre root of n
Packit 40b132
    if (i > n/i) break;
Packit 40b132
Packit 40b132
    // Now, we have i <= n/i < n.
Packit 40b132
    // If n is divisible by i, n is not prime.
Packit 40b132
    if (n % i == 0) return false;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  // n has no integer factor in the range (1, n), and thus is prime.
Packit 40b132
  return true;
Packit 40b132
}