<commit_before>require("../env");
require("../../d3");

var vows = require("vows"),
    assert = require("assert");

var suite = vows.describe("d3.scale.linear");

suite.addBatch({
  "linear": {
    topic: function() {
      return d3.scale.linear;
    },

    "domain": {
      "defaults to [0, 1]": function(linear) {
        var x = linear();
        assert.deepEqual(x.domain(), [0, 1]);
        assert.inDelta(x(.5), .5, 1e-6);
      },
      "coerces domain values to numbers": function(linear) {
        var x = linear().domain([new Date(1990, 0, 1), new Date(1991, 0, 1)]);
        assert.equal(typeof x.domain()[0], "number");
        assert.equal(typeof x.domain()[1], "number");
        assert.inDelta(x(new Date(1989, 09, 20)), -.2, 1e-2);
        assert.inDelta(x(new Date(1990, 00, 01)), 0, 1e-2);
        assert.inDelta(x(new Date(1990, 02, 15)), .2, 1e-2);
        assert.inDelta(x(new Date(1990, 04, 27)), .4, 1e-2);
        assert.inDelta(x(new Date(1991, 00, 01)), 1, 1e-2);
        assert.inDelta(x(new Date(1991, 02, 15)), 1.2, 1e-2);
        var x = linear().domain(["0", "1"]);
        assert.equal(typeof x.domain()[0], "number");
        assert.equal(typeof x.domain()[1], "number");
        assert.inDelta(x(.5), .5, 1e-6);
        var x = linear().domain([new Number(0), new Number(1)]);
        assert.equal(typeof x.domain()[0], "number");
        assert.equal(typeof x.domain()[1], "number");
        assert.inDelta(x(.5), .5, 1e-6);
      },
      "can specify a polylinear domain and range": function(linear) {
        var x = linear().domain([-10, 0, 100]).range(["red", "white", "green"]);
        assert.equal(x(-5), "#ff8080");
        assert.equal(x(50), "#80c080");
        assert.equal(x(75), "#40a040");
      },
      "an empty domain maps to the range start": function(linear) {
        var x = linear().domain([0, 0]).range(["red", "green"]);
        assert.equal(x(0), "#ff0000");
        assert.equal(x(-1), "#ff0000");
        assert.equal(x(1), "#ff0000");
      }
    },

    "range": {
      "defaults to [0, 1]": function(linear) {
        var x = linear();
        assert.deepEqual(x.range(), [0, 1]);
        assert.inDelta(x.invert(.5), .5, 1e-6);
      },
      "does not coerce range to numbers": function(linear) {
        var x = linear().range(["0", "2"]);
        assert.equal(typeof x.range()[0], "string");
        assert.equal(typeof x.range()[1], "string");
      },
      "can specify range values as colors": function(linear) {
        var x = linear().range(["red", "blue"]);
        assert.equal(x(.5), "#800080");
        var x = linear().range(["#ff0000", "#0000ff"]);
        assert.equal(x(.5), "#800080");
        var x = linear().range(["#f00", "#00f"]);
        assert.equal(x(.5), "#800080");
        var x = linear().range([d3.rgb(255,0,0), d3.hsl(240,1,.5)]);
        assert.equal(x(.5), "#800080");
        var x = linear().range(["hsl(0,100%,50%)", "hsl(240,100%,50%)"]);
        assert.equal(x(.5), "#800080");
      },
      "can specify range values as arrays or objects": function(linear) {
        var x = linear().range([{color: "red"}, {color: "blue"}]);
        assert.deepEqual(x(.5), {color: "#800080"});
        var x = linear().range([["red"], ["blue"]]);
        assert.deepEqual(x(.5), ["#800080"]);
      }
    },

    "interpolate": {
      "defaults to d3.interpolate": function(linear) {
        var x = linear().range(["red", "blue"]);
        assert.equal(x.interpolate(), d3.interpolate);
        assert.equal(x(.5), "#800080");
      },
      "can specify a custom interpolator": function(linear) {
        var x = linear().range(["red", "blue"]).interpolate(d3.interpolateHsl);
        assert.equal(x(.5), "#00ff00");
      }
    },

    "clamp": {
      "defaults to false": function(linear) {
        var x = linear();
        assert.isFalse(x.clamp());
        assert.inDelta(x(-.5), -.5, 1e-6);
        assert.inDelta(x(1.5), 1.5, 1e-6);
      },
      "can clamp to the domain": function(linear) {
        var x = linear().clamp(true);
        assert.inDelta(x(-.5), 0, 1e-6);
        assert.inDelta(x(.5), .5, 1e-6);
        assert.inDelta(x(1.5), 1, 1e-6);
        var x = linear().domain([1, 0]).clamp(true);
        assert.inDelta(x(-.5), 1, 1e-6);
        assert.inDelta(x(.5), .5, 1e-6);
        assert.inDelta(x(1.5), 0, 1e-6);
      }
    },

    "maps a number to a number": function(linear) {
      var x = linear().domain([1, 2]);
      assert.inDelta(x(.5), -.5, 1e-6);
      assert.inDelta(x(1), 0, 1e-6);
      assert.inDelta(x(1.5), .5, 1e-6);
      assert.inDelta(x(2), 1, 1e-6);
      assert.inDelta(x(2.5), 1.5, 1e-6);
    },

    "invert": {
      "maps a number to a number": function(linear) {
        var x = linear().range([1, 2]);
        assert.inDelta(x.invert(.5), -.5, 1e-6);<commit_msg>Add test for range clamping.<commit_after>require("../env");
require("../../d3");

var vows = require("vows"),
    assert = require("assert");

var suite = vows.describe("d3.scale.linear");

suite.addBatch({
  "linear": {
    topic: function() {
      return d3.scale.linear;
    },

    "domain": {
      "defaults to [0, 1]": function(linear) {
        var x = linear();
        assert.deepEqual(x.domain(), [0, 1]);
        assert.inDelta(x(.5), .5, 1e-6);
      },
      "coerces domain values to numbers": function(linear) {
        var x = linear().domain([new Date(1990, 0, 1), new Date(1991, 0, 1)]);
        assert.equal(typeof x.domain()[0], "number");
        assert.equal(typeof x.domain()[1], "number");
        assert.inDelta(x(new Date(1989, 09, 20)), -.2, 1e-2);
        assert.inDelta(x(new Date(1990, 00, 01)), 0, 1e-2);
        assert.inDelta(x(new Date(1990, 02, 15)), .2, 1e-2);
        assert.inDelta(x(new Date(1990, 04, 27)), .4, 1e-2);
        assert.inDelta(x(new Date(1991, 00, 01)), 1, 1e-2);
        assert.inDelta(x(new Date(1991, 02, 15)), 1.2, 1e-2);
        var x = linear().domain(["0", "1"]);
        assert.equal(typeof x.domain()[0], "number");
        assert.equal(typeof x.domain()[1], "number");
        assert.inDelta(x(.5), .5, 1e-6);
        var x = linear().domain([new Number(0), new Number(1)]);
        assert.equal(typeof x.domain()[0], "number");
        assert.equal(typeof x.domain()[1], "number");
        assert.inDelta(x(.5), .5, 1e-6);
      },
      "can specify a polylinear domain and range": function(linear) {
        var x = linear().domain([-10, 0, 100]).range(["red", "white", "green"]);
        assert.equal(x(-5), "#ff8080");
        assert.equal(x(50), "#80c080");
        assert.equal(x(75), "#40a040");
      },
      "an empty domain maps to the range start": function(linear) {
        var x = linear().domain([0, 0]).range(["red", "green"]);
        assert.equal(x(0), "#ff0000");
        assert.equal(x(-1), "#ff0000");
        assert.equal(x(1), "#ff0000");
      }
    },

    "range": {
      "defaults to [0, 1]": function(linear) {
        var x = linear();
        assert.deepEqual(x.range(), [0, 1]);
        assert.inDelta(x.invert(.5), .5, 1e-6);
      },
      "does not coerce range to numbers": function(linear) {
        var x = linear().range(["0", "2"]);
        assert.equal(typeof x.range()[0], "string");
        assert.equal(typeof x.range()[1], "string");
      },
      "can specify range values as colors": function(linear) {
        var x = linear().range(["red", "blue"]);
        assert.equal(x(.5), "#800080");
        var x = linear().range(["#ff0000", "#0000ff"]);
        assert.equal(x(.5), "#800080");
        var x = linear().range(["#f00", "#00f"]);
        assert.equal(x(.5), "#800080");
        var x = linear().range([d3.rgb(255,0,0), d3.hsl(240,1,.5)]);
        assert.equal(x(.5), "#800080");
        var x = linear().range(["hsl(0,100%,50%)", "hsl(240,100%,50%)"]);
        assert.equal(x(.5), "#800080");
      },
      "can specify range values as arrays or objects": function(linear) {
        var x = linear().range([{color: "red"}, {color: "blue"}]);
        assert.deepEqual(x(.5), {color: "#800080"});
        var x = linear().range([["red"], ["blue"]]);
        assert.deepEqual(x(.5), ["#800080"]);
      }
    },

    "interpolate": {
      "defaults to d3.interpolate": function(linear) {
        var x = linear().range(["red", "blue"]);
        assert.equal(x.interpolate(), d3.interpolate);
        assert.equal(x(.5), "#800080");
      },
      "can specify a custom interpolator": function(linear) {
        var x = linear().range(["red", "blue"]).interpolate(d3.interpolateHsl);
        assert.equal(x(.5), "#00ff00");
      }
    },

    "clamp": {
      "defaults to false": function(linear) {
        var x = linear();
        assert.isFalse(x.clamp());
        assert.inDelta(x(-.5), -.5, 1e-6);
        assert.inDelta(x(1.5), 1.5, 1e-6);
      },
      "can clamp to the domain": function(linear) {
        var x = linear().clamp(true);
        assert.inDelta(x(-.5), 0, 1e-6);
        assert.inDelta(x(.5), .5, 1e-6);
        assert.inDelta(x(1.5), 1, 1e-6);
        var x = linear().domain([1, 0]).clamp(true);
        assert.inDelta(x(-.5), 1, 1e-6);
        assert.inDelta(x(.5), .5, 1e-6);
        assert.inDelta(x(1.5), 0, 1e-6);
      },
      "can clamp to the range": function(linear) {
        var x = linear().clamp(true);
        assert.inDelta(x.invert(-.5), 0, 1e-6);
        assert.inDelta(x.invert(.5), .5, 1e-6);
        assert.inDelta(x.invert(1.5), 1, 1e-6);
        var x = linear().range([1, 0]).clamp(true);
        assert.inDelta(x.invert(-.5), 1, 1e-6);
        assert.inDelta(x.invert(.5), .5, 1e-6);
        assert.inDelta(x.invert(1.5), 0, 1e-6);
      }
    },

    "maps a number to a number": function(linear) {
      var x = linear().domain([1, 2]);
      assert.inDelta(x(.5), -.5, 1e-6);
      assert.inDelta(x(1), 0, 1e-6);
      assert.inDelta(x(1.5), .5, 1e-6);
      assert.inDelta(x(2), 1, 1e-6);
      assert.inDelta(x(2.5), 1.5, 1e-6);
    },

    "invert": {
      "maps a number to a number": function(linear) {
        var x = linear().range([1, 2]);
        assert.inDelta(x.invert(.5), -.5, 1e-6);
{'fraction_non_alphanumeric': 0.2278894721204318, 'fraction_numerical': 0.06873328711498465, 'mean_word_length': 2.413793103448276, 'pattern_counts': {'":': 41, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '7118233', 'n_tokens_mistral': 4062, 'n_tokens_neox': 3635, 'n_words': 760}