From 6d0447a02fe2aa1c6a926bf5e4f6423045095a11 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 26 Jun 2021 15:23:16 +0100 Subject: [PATCH] Vector3: add clone --- .tests/Vector3/area.test.lua | 9 --------- .tests/Vector3/ceil.test.lua | 9 --------- .tests/Vector3/clone.test.lua | 24 ++++++++++++++++++++++++ .tests/Vector3/floor.test.lua | 9 --------- .tests/Vector3/length.test.lua | 8 -------- .tests/Vector3/length_squared.test.lua | 9 --------- .tests/Vector3/round.test.lua | 9 --------- .tests/Vector3/sqrt.test.lua | 9 --------- .tests/Vector3/tostring.test.lua | 9 --------- worldeditadditions/utils/vector3.lua | 7 +++++++ 10 files changed, 31 insertions(+), 71 deletions(-) create mode 100644 .tests/Vector3/clone.test.lua diff --git a/.tests/Vector3/area.test.lua b/.tests/Vector3/area.test.lua index d593fd4..d7fc97f 100644 --- a/.tests/Vector3/area.test.lua +++ b/.tests/Vector3/area.test.lua @@ -1,14 +1,5 @@ local Vector3 = require("worldeditadditions.utils.vector3") -local function format_map(map) - local result = {} - for key, value in pairs(map) do - table.insert(result, key.."\t"..tostring(value)) - end - return table.concat(result, "\n") -end - - describe("Vector3.area", function() it("should work with a positive vector", function() local a = Vector3.new(3, 3, 3) diff --git a/.tests/Vector3/ceil.test.lua b/.tests/Vector3/ceil.test.lua index 16ddd7e..449d89d 100644 --- a/.tests/Vector3/ceil.test.lua +++ b/.tests/Vector3/ceil.test.lua @@ -1,14 +1,5 @@ local Vector3 = require("worldeditadditions.utils.vector3") -local function format_map(map) - local result = {} - for key, value in pairs(map) do - table.insert(result, key.."\t"..tostring(value)) - end - return table.concat(result, "\n") -end - - describe("Vector3.ceil", function() it("should ceil a positive vector", function() local a = Vector3.new(3.1, 4.2, 5.8) diff --git a/.tests/Vector3/clone.test.lua b/.tests/Vector3/clone.test.lua new file mode 100644 index 0000000..361f9a2 --- /dev/null +++ b/.tests/Vector3/clone.test.lua @@ -0,0 +1,24 @@ +local Vector3 = require("worldeditadditions.utils.vector3") + +describe("Vector3.clone", function() + it("should return a new Vector3 instance", function() + local a = Vector3.new(3, 4, 5) + + local result = a:clone() + assert.are.same( + Vector3.new(3, 4, 5), + result + ) + assert.are_not.equal(result, a) + end) + it("should return a new Vector3 instance for a different vector", function() + local a = Vector3.new(-99, 66, 88) + + local result = a:clone() + assert.are.same( + Vector3.new(-99, 66, 88), + result + ) + assert.are_not.equal(result, a) + end) +end) diff --git a/.tests/Vector3/floor.test.lua b/.tests/Vector3/floor.test.lua index 4226475..c29b56d 100644 --- a/.tests/Vector3/floor.test.lua +++ b/.tests/Vector3/floor.test.lua @@ -1,14 +1,5 @@ local Vector3 = require("worldeditadditions.utils.vector3") -local function format_map(map) - local result = {} - for key, value in pairs(map) do - table.insert(result, key.."\t"..tostring(value)) - end - return table.concat(result, "\n") -end - - describe("Vector3.floor", function() it("should floor a positive vector", function() local a = Vector3.new(3.1, 4.75, 5.9) diff --git a/.tests/Vector3/length.test.lua b/.tests/Vector3/length.test.lua index 3d225b9..3c8924b 100644 --- a/.tests/Vector3/length.test.lua +++ b/.tests/Vector3/length.test.lua @@ -1,13 +1,5 @@ local Vector3 = require("worldeditadditions.utils.vector3") -local function format_map(map) - local result = {} - for key, value in pairs(map) do - table.insert(result, key.."\t"..tostring(value)) - end - return table.concat(result, "\n") -end - -- To find these numbers, in Javascript: -- function t(x) { return Math.sqrt((x*x)*3); } -- for(let i = 0; i < 1000000000; i++) { let r = t(i); if(Math.floor(r) === r) console.log(`i ${i}, r ${r}`); } diff --git a/.tests/Vector3/length_squared.test.lua b/.tests/Vector3/length_squared.test.lua index d3bb2a4..3e049c9 100644 --- a/.tests/Vector3/length_squared.test.lua +++ b/.tests/Vector3/length_squared.test.lua @@ -1,14 +1,5 @@ local Vector3 = require("worldeditadditions.utils.vector3") -local function format_map(map) - local result = {} - for key, value in pairs(map) do - table.insert(result, key.."\t"..tostring(value)) - end - return table.concat(result, "\n") -end - - describe("Vector3.length_squared", function() it("should work with a positive vector", function() local a = Vector3.new(3, 3, 3) diff --git a/.tests/Vector3/round.test.lua b/.tests/Vector3/round.test.lua index 9b673af..6107381 100644 --- a/.tests/Vector3/round.test.lua +++ b/.tests/Vector3/round.test.lua @@ -1,14 +1,5 @@ local Vector3 = require("worldeditadditions.utils.vector3") -local function format_map(map) - local result = {} - for key, value in pairs(map) do - table.insert(result, key.."\t"..tostring(value)) - end - return table.concat(result, "\n") -end - - describe("Vector3.round", function() it("should round a positive vector", function() local a = Vector3.new(3.1, 4.75, 5.9) diff --git a/.tests/Vector3/sqrt.test.lua b/.tests/Vector3/sqrt.test.lua index 6726a64..dbd84ca 100644 --- a/.tests/Vector3/sqrt.test.lua +++ b/.tests/Vector3/sqrt.test.lua @@ -1,14 +1,5 @@ local Vector3 = require("worldeditadditions.utils.vector3") -local function format_map(map) - local result = {} - for key, value in pairs(map) do - table.insert(result, key.."\t"..tostring(value)) - end - return table.concat(result, "\n") -end - - describe("Vector3.sqrt", function() it("should sqrt a positive vector", function() local a = Vector3.new(16, 64, 16) diff --git a/.tests/Vector3/tostring.test.lua b/.tests/Vector3/tostring.test.lua index e46884a..56f3dac 100644 --- a/.tests/Vector3/tostring.test.lua +++ b/.tests/Vector3/tostring.test.lua @@ -1,14 +1,5 @@ local Vector3 = require("worldeditadditions.utils.vector3") -local function format_map(map) - local result = {} - for key, value in pairs(map) do - table.insert(result, key.."\t"..tostring(value)) - end - return table.concat(result, "\n") -end - - describe("Vector3.__tostring", function() it("should stringify a Vector3", function() local a = Vector3.new(3, 4, 5) diff --git a/worldeditadditions/utils/vector3.lua b/worldeditadditions/utils/vector3.lua index 85a9019..e91aca1 100644 --- a/worldeditadditions/utils/vector3.lua +++ b/worldeditadditions/utils/vector3.lua @@ -21,6 +21,13 @@ function Vector3.new(x, y, z) return result end +--- Returns a new instance of this vector. +-- @param a Vector3 The vector to clone. +-- @returns Vector3 A new vector whose values are identical to those of the original vector. +function Vector3.clone(a) + return Vector3.new(a.x, a.y, a.z) +end + --- Adds the specified vectors or numbers together. -- Returns the result as a new vector. -- If 1 of the inputs is a number and the other a vector, then the number will