Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 committed Oct 8, 2024
1 parent 1c93102 commit 7817104
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 19 deletions.
166 changes: 166 additions & 0 deletions sdk/assemblyscript/src/assembly/__tests__/vectors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2024 Hypermode, Inc.
* Licensed under the terms of the Apache License, Version 2.0
* See the LICENSE file that accompanied this code for further details.
*
* SPDX-FileCopyrightText: 2024 Hypermode, Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/

import { expect, it, run } from "as-test";
import { vectors } from "..";

it("should add two vectors together", () => {
const a = [1, 2, 3];
const b = [4, 5, 6];
const result = vectors.add(a, b);
expect(result).toBe([5, 7, 9]);
});

it("should add two vectors together in place", () => {
const a = [1, 2, 3];
const b = [4, 5, 6];
vectors.addInPlace(a, b);
expect(a).toBe([5, 7, 9]);
});

it("should subtract two vectors", () => {
const a = [1, 2, 3];
const b = [4, 5, 6];
const result = vectors.subtract(a, b);
expect(result).toBe([-3, -3, -3]);
});

it("should subtract two vectors in place", () => {
const a = [1, 2, 3];
const b = [4, 5, 6];
vectors.subtractInPlace(a, b);
expect(a).toBe([-3, -3, -3]);
});

it("should add a number to a vector", () => {
const a = [1, 2, 3];
const b = 4;
const result = vectors.addNumber(a, b);
expect(result).toBe([5, 6, 7]);
});

it("should add a number to a vector in place", () => {
const a = [1, 2, 3];
const b = 4;
vectors.addNumberInPlace(a, b);
expect(a).toBe([5, 6, 7]);
});

it("should subtract a number from a vector", () => {
const a = [1, 2, 3];
const b = 4;
const result = vectors.subtractNumber(a, b);
expect(result).toBe([-3, -2, -1]);
});

it("should subtract a number from a vector in place", () => {
const a = [1, 2, 3];
const b = 4;
vectors.subtractNumberInPlace(a, b);
expect(a).toBe([-3, -2, -1]);
});

it("should multiply a vector by a number", () => {
const a = [1, 2, 3];
const b = 4;
const result = vectors.multiplyNumber(a, b);
expect(result).toBe([4, 8, 12]);
});

it("should multiply a vector by a number in place", () => {
const a = [1, 2, 3];
const b = 4;
vectors.multiplyNumberInPlace(a, b);
expect(a).toBe([4, 8, 12]);
});

it("should divide a vector by a number", () => {
const a = [4, 8, 12];
const b = 4;
const result = vectors.divideNumber(a, b);
expect(result).toBe([1, 2, 3]);
});

it("should divide a vector by a number in place", () => {
const a = [4, 8, 12];
const b = 4;
vectors.divideNumberInPlace(a, b);
expect(a).toBe([1, 2, 3]);
});

it("should compute the dot product of two vectors", () => {
const a = [1, 2, 3];
const b = [4, 5, 6];
const result = vectors.dot(a, b);
expect(result).toBe(32);
});

it("should compute the magnitude of a vector", () => {
const a = [1, 2, 3];
const result = vectors.magnitude(a);
expect(result).toBe(sqrt<f64>(14));
});

it("should normalize a vector", () => {
const a = [1, 2, 3];
const result = vectors.normalize(a);
const magnitude = vectors.magnitude(result);
expect(magnitude).toBe(1);
});

it("should compute the sum of a vector", () => {
const a = [1, 2, 3];
const result = vectors.sum(a);
expect(result).toBe(6);
});

it("should compute the product of a vector", () => {
const a = [1, 2, 3];
const result = vectors.product(a);
expect(result).toBe(6);
});

it("should compute the mean of a vector", () => {
const a = [1, 2, 3];
const result = vectors.mean(a);
expect(result).toBe(2);
});

it("should compute the min of a vector", () => {
const a = [1, 2, 3];
const result = vectors.min(a);
expect(result).toBe(1);
});

it("should compute the max of a vector", () => {
const a = [1, 2, 3];
const result = vectors.max(a);
expect(result).toBe(3);
});

it("should compute the absolute value of a vector", () => {
const a = [1, -2, 3];
const result = vectors.abs(a);
expect(result).toBe([1, 2, 3]);
});

it("should compute the absolute value of a vector in place", () => {
const a = [1, -2, 3];
vectors.absInPlace(a);
expect(a).toBe([1, 2, 3]);
});

it("should compute the euclidian distance between two vectors", () => {
const a = [1, 2, 3];
const b = [4, 5, 6];
const result = vectors.euclidianDistance(a, b);
expect(result).toBe(sqrt<f64>(27));
});

run();
3 changes: 3 additions & 0 deletions sdk/assemblyscript/src/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ export { collections };

import models from "./models";
export { models };

import * as vectors from "./vectors";
export { vectors };
33 changes: 14 additions & 19 deletions sdk/assemblyscript/src/assembly/vectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export function dot<T extends number>(a: T[], b: T[]): T {
checkValidArray(a);
checkValidArray(b);
assertEqualLength(a, b);
let result = 0;
let result: number = 0;
for (let i = 0; i < a.length; i++) {
result += a[i] * b[i];
}
Expand All @@ -237,9 +237,9 @@ export function dot<T extends number>(a: T[], b: T[]): T {
* @param a: The vector
* @returns: The magnitude of the vector
*/
export function magnitude<T extends number>(a: T[]): T {
export function magnitude<T extends number>(a: T[]): f64 {
checkValidArray(a);
return sqrt<number>(dot(a, a)) as T;
return sqrt<f64>(dot(a, a));
}

/**
Expand All @@ -248,19 +248,14 @@ export function magnitude<T extends number>(a: T[]): T {
* @param b: The second vector
* @returns: The cross product of the two vectors
*/
export function normalize<T extends number>(a: T[]): T[] {
checkValidArray(a);
return divideNumber(a, magnitude(a)) as T[];
}

/**
*
* Normalize a vector, modifying the first vector.
* @param a: The vector to normalize
*/
export function normalizeInPlace<T extends number>(a: T[]): void {
export function normalize<T extends number>(a: T[]): f64[] {
checkValidArray(a);
divideNumberInPlace(a, magnitude(a) as T);
const magnitudeValue = magnitude(a);
const result: f64[] = new Array<f64>(a.length);
for (let i = 0; i < a.length; i++) {
result[i] = (a[i] as f64) / magnitudeValue;
}
return result;
}

/**
Expand Down Expand Up @@ -299,9 +294,9 @@ export function product<T extends number>(a: T[]): T {
* @param a: The vector
* @returns: The mean of the vector
*/
export function mean<T extends number>(a: T[]): T {
export function mean<T extends number>(a: T[]): f64 {
checkValidArray(a);
return (sum(a) / a.length) as T;
return f64(sum(a)) / f64(a.length);
}

/**
Expand Down Expand Up @@ -372,11 +367,11 @@ export function absInPlace<T extends number>(a: T[]): void {
* @param b: The second vector
* @returns: The euclidian distance between the two vectors
*/
export function euclidianDistance<T extends number>(a: T[], b: T[]): T {
export function euclidianDistance<T extends number>(a: T[], b: T[]): f64 {
checkValidArray(a);
let sum = 0;
for (let i = 0; i < a.length; i++) {
sum += (a[i] - b[i]) ** 2;
}
return sqrt<number>(sum) as T;
return sqrt<f64>(sum);
}
17 changes: 17 additions & 0 deletions sdk/assemblyscript/src/tests/vectors.run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2024 Hypermode, Inc.
* Licensed under the terms of the Apache License, Version 2.0
* See the LICENSE file that accompanied this code for further details.
*
* SPDX-FileCopyrightText: 2024 Hypermode, Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/

import { readFileSync } from "fs";
import { instantiate } from "../build/vectors.spec.js";
const binary = readFileSync("./build/vectors.spec.wasm");
const module = new WebAssembly.Module(binary);
instantiate(module, {
env: {},
hypermode: {},
});

0 comments on commit 7817104

Please sign in to comment.