feat: added support to wasm (#94)
* test: run tests also in wasm targets * fix: install rand with wasm-bindgen por wasm targets * fix: use actual usize size to access buffer. * fix: do not run functions that create files in wasm. * test: do not run in wasm test that panics. Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
This commit is contained in:
@@ -56,9 +56,11 @@ pub fn load_dataset() -> Dataset<f32, f32> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use super::super::*;
|
||||
use super::*;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn refresh_boston_dataset() {
|
||||
@@ -67,6 +69,7 @@ mod tests {
|
||||
assert!(serialize_data(&dataset, "boston.xy").is_ok());
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn boston_dataset() {
|
||||
let dataset = load_dataset();
|
||||
|
||||
@@ -66,17 +66,20 @@ pub fn load_dataset() -> Dataset<f32, f32> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use super::super::*;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn refresh_cancer_dataset() {
|
||||
// run this test to generate breast_cancer.xy file.
|
||||
let dataset = load_dataset();
|
||||
assert!(serialize_data(&dataset, "breast_cancer.xy").is_ok());
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn cancer_dataset() {
|
||||
let dataset = load_dataset();
|
||||
|
||||
@@ -50,9 +50,11 @@ pub fn load_dataset() -> Dataset<f32, f32> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use super::super::*;
|
||||
use super::*;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn refresh_diabetes_dataset() {
|
||||
@@ -61,6 +63,7 @@ mod tests {
|
||||
assert!(serialize_data(&dataset, "diabetes.xy").is_ok());
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn boston_dataset() {
|
||||
let dataset = load_dataset();
|
||||
|
||||
@@ -45,9 +45,11 @@ pub fn load_dataset() -> Dataset<f32, f32> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use super::super::*;
|
||||
use super::*;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn refresh_digits_dataset() {
|
||||
@@ -55,7 +57,7 @@ mod tests {
|
||||
let dataset = load_dataset();
|
||||
assert!(serialize_data(&dataset, "digits.xy").is_ok());
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn digits_dataset() {
|
||||
let dataset = load_dataset();
|
||||
|
||||
@@ -137,6 +137,7 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn test_make_blobs() {
|
||||
let dataset = make_blobs(10, 2, 3);
|
||||
@@ -149,6 +150,7 @@ mod tests {
|
||||
assert_eq!(dataset.num_samples, 10);
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn test_make_circles() {
|
||||
let dataset = make_circles(10, 0.5, 0.05);
|
||||
@@ -161,6 +163,7 @@ mod tests {
|
||||
assert_eq!(dataset.num_samples, 10);
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn test_make_moons() {
|
||||
let dataset = make_moons(10, 0.05);
|
||||
|
||||
@@ -50,9 +50,11 @@ pub fn load_dataset() -> Dataset<f32, f32> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use super::super::*;
|
||||
use super::*;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn refresh_iris_dataset() {
|
||||
@@ -61,6 +63,7 @@ mod tests {
|
||||
assert!(serialize_data(&dataset, "iris.xy").is_ok());
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn iris_dataset() {
|
||||
let dataset = load_dataset();
|
||||
|
||||
+8
-2
@@ -8,9 +8,12 @@ pub mod digits;
|
||||
pub mod generator;
|
||||
pub mod iris;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use crate::math::num::RealNumber;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::io::prelude::*;
|
||||
|
||||
/// Dataset
|
||||
@@ -49,6 +52,8 @@ impl<X, Y> Dataset<X, Y> {
|
||||
}
|
||||
}
|
||||
|
||||
// Running this in wasm throws: operation not supported on this platform.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn serialize_data<X: RealNumber, Y: RealNumber>(
|
||||
dataset: &Dataset<X, Y>,
|
||||
@@ -85,9 +90,9 @@ pub(crate) fn deserialize_data(
|
||||
const USIZE_SIZE: usize = std::mem::size_of::<usize>();
|
||||
let (num_samples, num_features) = {
|
||||
let mut buffer = [0u8; USIZE_SIZE];
|
||||
buffer.copy_from_slice(&bytes[0..8]);
|
||||
buffer.copy_from_slice(&bytes[0..USIZE_SIZE]);
|
||||
let num_features = usize::from_le_bytes(buffer);
|
||||
buffer.copy_from_slice(&bytes[8..16]);
|
||||
buffer.copy_from_slice(&bytes[8..8 + USIZE_SIZE]);
|
||||
let num_samples = usize::from_le_bytes(buffer);
|
||||
(num_samples, num_features)
|
||||
};
|
||||
@@ -116,6 +121,7 @@ pub(crate) fn deserialize_data(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn as_matrix() {
|
||||
let dataset = Dataset {
|
||||
|
||||
Reference in New Issue
Block a user