<commit_before>// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors,
// refer to the Cargo.toml file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[macro_use]
extern crate cgmath;

use cgmath::*;
use std::f64;

#[test]
fn test_constructor() {
    assert_eq!(vec2(1f32, 2f32), Vector2::new(1f32, 2f32));
    assert_eq!(vec3(1f64, 2f64, 3f64), Vector3::new(1f64, 2f64, 3f64));
    assert_eq!(vec4(1isize, 2isize, 3isize, 4isize), Vector4::new(1isize, 2isize, 3isize, 4isize));
}

#[test]
fn test_from_value() {
    assert_eq!(Vector2::from_value(102isize), Vector2::new(102isize, 102isize));
    assert_eq!(Vector3::from_value(22isize), Vector3::new(22isize, 22isize, 22isize));
    assert_eq!(Vector4::from_value(76.5f64), Vector4::new(76.5f64, 76.5f64, 76.5f64, 76.5f64));
}

macro_rules! test_op_add {
    ($VectorN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (
        // vector + vector ops
        assert_eq!($v + $v, $VectorN::new($($v.$field + $v.$field),+));
        assert_eq!(&$v + &$v, $v + $v);
        assert_eq!(&$v + $v, $v + $v);
        assert_eq!($v + &$v, $v + $v);
        // vector + scalar ops
        assert_eq!($v + $s, $VectorN::new($($v.$field + $s),+));
        assert_eq!($s + $v, $VectorN::new($($s + $v.$field),+));
        assert_eq!(&$v + $s, $v + $s);
        assert_eq!($s + &$v, $s + $v);
        // commutativity
        assert_eq!($v + $s, $s + $v);
    )
}

macro_rules! test_op_sub {
    ($VectorN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (
        // vector - vector ops
        assert_eq!($v - $v, $VectorN::new($($v.$field - $v.$field),+));
        assert_eq!(&$v - &$v, $v - $v);
        assert_eq!(&$v - $v, $v - $v);
        assert_eq!($v - &$v, $v - $v);
        // vector - scalar ops
        assert_eq!($v - $s, $VectorN::new($($v.$field - $s),+));
        assert_eq!($s - $v, $VectorN::new($($s - $v.$field),+));
        assert_eq!(&$v - $s, $v - $s);
        assert_eq!($s - &$v, $s - $v);
        // commutativity
        assert_eq!($v - $s, -($s - $v));
    )
}

macro_rules! test_op_mul {
    ($VectorN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (
        // vector * vector ops
        assert_eq!($v * $v, $VectorN::new($($v.$field * $v.$field),+));
        assert_eq!(&$v * &$v, $v * $v);
        assert_eq!(&$v * $v, $v * $v);
        assert_eq!($v * &$v, $v * $v);
        // vector * scalar ops
        assert_eq!($v * $s, $VectorN::new($($v.$field * $s),+));
        assert_eq!($s * $v, $VectorN::new($($s * $v.$field),+));
        assert_eq!(&$v * $s, $v * $s);
        assert_eq!($s * &$v, $s * $v);
        // commutativity
        assert_eq!($v * $s, $s * $v);
    )
}

macro_rules! test_op_div {
    ($VectorN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (
        // vector / vector ops
        assert_eq!($v / $v, $VectorN::new($($v.$field / $v.$field),+));
        assert_eq!(&$v / &$v, $v / $v);
        assert_eq!(&$v / $v, $v / $v);
        assert_eq!($v / &$v, $v / $v);
        // vector / scalar ops
        assert_eq!($v / $s, $VectorN::new($($v.$field / $s),+));
        assert_eq!($s / $v, $VectorN::new($($s / $v.$field),+));
        assert_eq!(&$v / $s, $v / $s);
        assert_eq!($s / &$v, $s / $v);
    )
}

macro_rules! test_op_rem {
    ($VectorN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (
        // vector % vector ops
        assert_eq!($v % $v, $VectorN::new($($v.$field % $v.$field),+));
        assert_eq!(&$v % &$v, $v % $v);
        assert_eq!(&$v % $v, $v % $v);
        assert_eq!($v % &$v, $v % $v);
        // vector % scalar ops
        assert_eq!($v % $s, $VectorN::new($($v.$field % $s),+));
        assert_eq!($s % $v, $VectorN::new($($s % $v.$field),+));
        assert_eq!(&$v % $s, $v % $s);
        assert_eq!($s % &$v, $s % $v);
    )
}

macro_rules! test_ops {
    ($VectorN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (
        test_op_add!($VectorN { $($field),+ }, $s, $v);
        test_op_sub!($VectorN { $($field),+ }, $s, $v);
        test_op_mul!($VectorN { $($field),+ }, $s, $v);
        test_op_div!($VectorN { $($field),+ }, $s, $v);
        test_op_rem!($VectorN { $($field),+ }, $s, $v);
    )
}

#[test]
fn test_ops() {
    let a = [3.0f32, 4.0f32, 5.0f32, 6.0f32];
    let s = 2.0f32;

    let v4 = Vector4::from(a);
    test_ops!(Vector4 { x, y, z, w }, s, v4);

    let v3 = v4.truncate();
    test_ops!(Vector3 { x, y, z }, s, v3);

    let v2 = v3.truncate();
    test_ops!(Vector2 { x, y }, s, v2);
}

#[test]
fn test_dot() {
    assert_eq!(Vector2::new(1isize, 2isize).dot(Vector2::new(3isize, 4isize)), 11isize);
    assert_eq!(Vector3::new(1isize, 2isize, 3isize).dot(Vector3::new(4isize, 5isize, 6isize)), 32isize);<commit_msg>Split arithmetic operator tests into per op tests<commit_after>// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors,
// refer to the Cargo.toml file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[macro_use]
extern crate cgmath;

use cgmath::*;
use std::f64;

#[test]
fn test_constructor() {
    assert_eq!(vec2(1f32, 2f32), Vector2::new(1f32, 2f32));
    assert_eq!(vec3(1f64, 2f64, 3f64), Vector3::new(1f64, 2f64, 3f64));
    assert_eq!(vec4(1isize, 2isize, 3isize, 4isize), Vector4::new(1isize, 2isize, 3isize, 4isize));
}

#[test]
fn test_from_value() {
    assert_eq!(Vector2::from_value(102isize), Vector2::new(102isize, 102isize));
    assert_eq!(Vector3::from_value(22isize), Vector3::new(22isize, 22isize, 22isize));
    assert_eq!(Vector4::from_value(76.5f64), Vector4::new(76.5f64, 76.5f64, 76.5f64, 76.5f64));
}

macro_rules! impl_test_add {
    ($VectorN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (
        // vector + vector ops
        assert_eq!($v + $v, $VectorN::new($($v.$field + $v.$field),+));
        assert_eq!(&$v + &$v, $v + $v);
        assert_eq!(&$v + $v, $v + $v);
        assert_eq!($v + &$v, $v + $v);
        // vector + scalar ops
        assert_eq!($v + $s, $VectorN::new($($v.$field + $s),+));
        assert_eq!($s + $v, $VectorN::new($($s + $v.$field),+));
        assert_eq!(&$v + $s, $v + $s);
        assert_eq!($s + &$v, $s + $v);
        // commutativity
        assert_eq!($v + $s, $s + $v);
    )
}

macro_rules! impl_test_sub {
    ($VectorN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (
        // vector - vector ops
        assert_eq!($v - $v, $VectorN::new($($v.$field - $v.$field),+));
        assert_eq!(&$v - &$v, $v - $v);
        assert_eq!(&$v - $v, $v - $v);
        assert_eq!($v - &$v, $v - $v);
        // vector - scalar ops
        assert_eq!($v - $s, $VectorN::new($($v.$field - $s),+));
        assert_eq!($s - $v, $VectorN::new($($s - $v.$field),+));
        assert_eq!(&$v - $s, $v - $s);
        assert_eq!($s - &$v, $s - $v);
        // commutativity
        assert_eq!($v - $s, -($s - $v));
    )
}

macro_rules! impl_test_mul {
    ($VectorN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (
        // vector * vector ops
        assert_eq!($v * $v, $VectorN::new($($v.$field * $v.$field),+));
        assert_eq!(&$v * &$v, $v * $v);
        assert_eq!(&$v * $v, $v * $v);
        assert_eq!($v * &$v, $v * $v);
        // vector * scalar ops
        assert_eq!($v * $s, $VectorN::new($($v.$field * $s),+));
        assert_eq!($s * $v, $VectorN::new($($s * $v.$field),+));
        assert_eq!(&$v * $s, $v * $s);
        assert_eq!($s * &$v, $s * $v);
        // commutativity
        assert_eq!($v * $s, $s * $v);
    )
}

macro_rules! impl_test_div {
    ($VectorN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (
        // vector / vector ops
        assert_eq!($v / $v, $VectorN::new($($v.$field / $v.$field),+));
        assert_eq!(&$v / &$v, $v / $v);
        assert_eq!(&$v / $v, $v / $v);
        assert_eq!($v / &$v, $v / $v);
        // vector / scalar ops
        assert_eq!($v / $s, $VectorN::new($($v.$field / $s),+));
        assert_eq!($s / $v, $VectorN::new($($s / $v.$field),+));
        assert_eq!(&$v / $s, $v / $s);
        assert_eq!($s / &$v, $s / $v);
    )
}

macro_rules! impl_test_rem {
    ($VectorN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (
        // vector % vector ops
        assert_eq!($v % $v, $VectorN::new($($v.$field % $v.$field),+));
        assert_eq!(&$v % &$v, $v % $v);
        assert_eq!(&$v % $v, $v % $v);
        assert_eq!($v % &$v, $v % $v);
        // vector % scalar ops
        assert_eq!($v % $s, $VectorN::new($($v.$field % $s),+));
        assert_eq!($s % $v, $VectorN::new($($s % $v.$field),+));
        assert_eq!(&$v % $s, $v % $s);
        assert_eq!($s % &$v, $s % $v);
    )
}

#[test]
fn test_add() {
    impl_test_add!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0, 6.0, 8.0));
    impl_test_add!(Vector3 { x, y, z }, 2.0f32, vec3(2.0f32, 4.0, 6.0));
    impl_test_add!(Vector2 { x, y }, 2.0f32, vec2(2.0f32, 4.0));
}

#[test]
fn test_sub() {
    impl_test_sub!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0, 6.0, 8.0));
    impl_test_sub!(Vector3 { x, y, z }, 2.0f32, vec3(2.0f32, 4.0, 6.0));
    impl_test_sub!(Vector2 { x, y }, 2.0f32, vec2(2.0f32, 4.0));
}

#[test]
fn test_mul() {
    impl_test_mul!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0, 6.0, 8.0));
    impl_test_mul!(Vector3 { x, y, z }, 2.0f32, vec3(2.0f32, 4.0, 6.0));
    impl_test_mul!(Vector2 { x, y }, 2.0f32, vec2(2.0f32, 4.0));
}

#[test]
fn test_div() {
    impl_test_div!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0, 6.0, 8.0));
    impl_test_div!(Vector3 { x, y, z }, 2.0f32, vec3(2.0f32, 4.0, 6.0));
    impl_test_div!(Vector2 { x, y }, 2.0f32, vec2(2.0f32, 4.0));
}

#[test]
fn test_rem() {
    impl_test_rem!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0, 6.0, 8.0));
    impl_test_rem!(Vector3 { x, y, z }, 2.0f32, vec3(2.0f32, 4.0, 6.0));
    impl_test_rem!(Vector2 { x, y }, 2.0f32, vec2(2.0f32, 4.0));
}

#[test]
fn test_dot() {
    assert_eq!(Vector2::new(1isize, 2isize).dot(Vector2::new(3isize, 4isize)), 11isize);
    assert_eq!(Vector3::new(1isize, 2isize, 3isize).dot(Vector3::new(4isize, 5isize, 6isize)), 32isize);
{'fraction_non_alphanumeric': 0.2380611056356957, 'fraction_numerical': 0.04390287608637219, 'mean_word_length': 2.7850118684299763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1150252', 'n_tokens_mistral': 4905, 'n_tokens_neox': 4648, 'n_words': 996}