var vows = require("vows"),
    load = require("../load"),
    assert = require("../assert"),
    OneTimeNumber = require("./one-time-number");

var suite = vows.describe("d3.mean");

suite.addBatch({
  "mean": {
    topic: load("arrays/mean").expression("d3.mean"),
    "returns the mean value for numbers": function(mean) {
      assert.equal(mean([1]), 1);
      assert.equal(mean([5, 1, 2, 3, 4]), 3);
      assert.equal(mean([20, 3]), 11.5);
      assert.equal(mean([3, 20]), 11.5);
    },
    "ignores null, undefined and NaN": function(mean) {
      assert.equal(mean([NaN, 1, 2, 3, 4, 5]), 3);
      assert.equal(mean([1, 2, 3, 4, 5, NaN]), 3);
      assert.equal(mean([10, null, 3, undefined, 5, NaN]), 6);
    },
    "returns undefined for empty array": function(mean) {
      assert.isUndefined(mean([]));
      assert.isUndefined(mean([null]));
      assert.isUndefined(mean([undefined]));
      assert.isUndefined(mean([NaN]));
      assert.isUndefined(mean([NaN, NaN]));
    },
    "applies the optional accessor function": function(mean) {
      assert.equal(mean([[1, 2, 3, 4, 5], [2, 4, 6, 8, 10]], function(d) { return mean(d); }), 4.5);
      assert.equal(mean([1, 2, 3, 4, 5], function(d, i) { return i; }), 2);
    },
    "coerces values to numbers": function(mean) {
      assert.equal(mean(["1"]), 1);
      assert.equal(mean(["5", "1", "2", "3", "4"]), 3);
      assert.equal(mean(["20", "3"]), 11.5);
      assert.equal(mean(["3", "20"]), 11.5);
    },
    "coerces values exactly once": function(mean) {
      var array = [1, new OneTimeNumber(3)];
      assert.equal(mean(array), 2);
      assert.equal(mean(array), 1);
    }
  }
});

suite.export(module);
