From c4ef9046d4af0ea09c0d28d513a76ed16e543a9b Mon Sep 17 00:00:00 2001 From: Augusto Date: Sat, 26 Dec 2020 22:46:07 +0000 Subject: [PATCH] Add subject and test for the exercise `circle` --- rust/tests/circle_test/Cargo.lock | 426 +++++++++++++++++++++++++++++ rust/tests/circle_test/Cargo.toml | 10 + rust/tests/circle_test/src/main.rs | 70 +++++ subjects/circle/README.md | 95 +++++++ 4 files changed, 601 insertions(+) create mode 100644 rust/tests/circle_test/Cargo.lock create mode 100644 rust/tests/circle_test/Cargo.toml create mode 100644 rust/tests/circle_test/src/main.rs create mode 100644 subjects/circle/README.md diff --git a/rust/tests/circle_test/Cargo.lock b/rust/tests/circle_test/Cargo.lock new file mode 100644 index 00000000..72a8b713 --- /dev/null +++ b/rust/tests/circle_test/Cargo.lock @@ -0,0 +1,426 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "autocfg" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" + +[[package]] +name = "bitflags" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" + +[[package]] +name = "byteorder" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855" + +[[package]] +name = "byteorder" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" + +[[package]] +name = "cc" +version = "1.0.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bbb73db36c1246e9034e307d0fba23f9a2e251faa47ade70c1bd252220c8311" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "circle" +version = "0.1.0" +dependencies = [ + "raster", +] + +[[package]] +name = "circle_test" +version = "0.1.0" +dependencies = [ + "circle", +] + +[[package]] +name = "color_quant" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dbbb57365263e881e805dc77d94697c9118fd94d8da011240555aa7b23445bd" + +[[package]] +name = "crossbeam-deque" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "lazy_static", + "maybe-uninit", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-queue" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + +[[package]] +name = "either" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" + +[[package]] +name = "enum_primitive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4551092f4d519593039259a9ed8daedf0da12e5109c5280338073eaeb81180" +dependencies = [ + "num-traits 0.1.43", +] + +[[package]] +name = "flate2" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6234dd4468ae5d1e2dbb06fe2b058696fdc50a339c68a393aefbf00bc81e423" +dependencies = [ + "libc", + "miniz-sys", +] + +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + +[[package]] +name = "gif" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e41945ba23db3bf51b24756d73d81acb4f28d85c3dccc32c6fae904438c25f" +dependencies = [ + "color_quant", + "lzw", +] + +[[package]] +name = "glob" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" + +[[package]] +name = "hermit-abi" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71" +dependencies = [ + "libc", +] + +[[package]] +name = "image" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76df2dce95fef56fd35dbc41c36e37b19aede703c6be7739e8b65d5788ffc728" +dependencies = [ + "byteorder 0.5.3", + "enum_primitive", + "glob", + "jpeg-decoder", + "num-iter", + "num-rational", + "num-traits 0.1.43", +] + +[[package]] +name = "inflate" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e0062d2dc2f17d2f13750d95316ae8a2ff909af0fda957084f5defd87c43bb" + +[[package]] +name = "jpeg-decoder" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b47b4c4e017b01abdc5bcc126d2d1002e5a75bbe3ce73f9f4f311a916363704" +dependencies = [ + "byteorder 1.3.4", + "rayon", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" + +[[package]] +name = "lzw" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" + +[[package]] +name = "maybe-uninit" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" + +[[package]] +name = "memoffset" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4fc2c02a7e374099d4ee95a193111f72d2110197fe200272371758f6c3643d8" +dependencies = [ + "autocfg", +] + +[[package]] +name = "miniz-sys" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e9e3ae51cea1576ceba0dde3d484d30e6e5b86dee0b2d412fe3a16a15c98202" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "num-bigint" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e63899ad0da84ce718c14936262a41cee2c79c981fc0a0e7c7beb47d5a07e8c1" +dependencies = [ + "num-integer", + "num-traits 0.2.12", + "rand", + "rustc-serialize", +] + +[[package]] +name = "num-integer" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" +dependencies = [ + "autocfg", + "num-traits 0.2.12", +] + +[[package]] +name = "num-iter" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6e6b7c748f995c4c29c5f5ae0248536e04a5739927c74ec0fa564805094b9f" +dependencies = [ + "autocfg", + "num-integer", + "num-traits 0.2.12", +] + +[[package]] +name = "num-rational" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee314c74bd753fc86b4780aa9475da469155f3848473a261d2d18e35245a784e" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits 0.2.12", + "rustc-serialize", +] + +[[package]] +name = "num-traits" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" +dependencies = [ + "num-traits 0.2.12", +] + +[[package]] +name = "num-traits" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "png" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06208e2ee243e3118a55dda9318f821f206d8563fb8d4df258767f8e62bb0997" +dependencies = [ + "bitflags", + "flate2", + "inflate", + "num-iter", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + +[[package]] +name = "raster" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c3f9e9cfa4260e25ea0d3bc72f3724afbeb65a61f8cb4d38c1d4de5309cd7ef" +dependencies = [ + "gif", + "image", + "png", +] + +[[package]] +name = "rayon" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db6ce3297f9c85e16621bb8cca38a06779ffc31bb8184e1be4bed2be4678a098" +dependencies = [ + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08a89b46efaf957e52b18062fb2f4660f8b8a4dde1807ca002690868ef2c85a9" +dependencies = [ + "crossbeam-deque", + "crossbeam-queue", + "crossbeam-utils", + "lazy_static", + "num_cpus", +] + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "winapi" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/rust/tests/circle_test/Cargo.toml b/rust/tests/circle_test/Cargo.toml new file mode 100644 index 00000000..a648751e --- /dev/null +++ b/rust/tests/circle_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "circle_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +circle = { path = "../../../../rust-piscine-solutions/circle"} \ No newline at end of file diff --git a/rust/tests/circle_test/src/main.rs b/rust/tests/circle_test/src/main.rs new file mode 100644 index 00000000..e05821ba --- /dev/null +++ b/rust/tests/circle_test/src/main.rs @@ -0,0 +1,70 @@ +//use raster::{Color, Image}; +use circle::{Circle, Point}; +// Create a structure that represents a circle +// using the radius and the center point + +fn main() { + let circle = Circle::new(500.0, 500.0, 150.0); + let circle1 = Circle { + center: Point { x: 80.0, y: 115.0 }, + radius: 30.0, + }; + let point_a = Point { x: 1.0, y: 1.0 }; + let point_b = Point { x: 0.0, y: 0.0 }; + println!("circle = {:?} area = {}", circle, circle.area()); + println!("circle = {:?} diameter = {}", circle, circle.diameter()); + println!("circle1 = {:?} diameter = {}", circle1, circle1.diameter()); + println!( + "circle and circle1 intersect = {}", + circle.intersect(&circle1) + ); + + println!( + "distance between {:?} and {:?} is {}", + point_a, + point_b, + point_a.distance(&point_b) + ); +} + +#[allow(dead_code)] +fn approx_eq(a: f64, b: f64) -> bool { + (a - b).abs() < f64::EPSILON +} + +#[test] +fn test_new_circle() { + let circle = Circle::new(500.0, 400.0, 150.0); + assert!(approx_eq(circle.radius, 150.0)); + assert!(approx_eq(circle.center.x, 500.0)); + assert!(approx_eq(circle.center.y, 400.0)); +} + +#[test] +fn test_distance() { + let a = Point { x: 0.0, y: 1.0 }; + let b = Point { x: 0.0, y: 0.0 }; + assert!(approx_eq(a.distance(&b), 1.0)); + let a = Point { x: 1.0, y: 0.0 }; + let b = Point { x: 0.0, y: 0.0 }; + assert!(approx_eq(a.distance(&b), 1.0)); + let a = Point { x: 1.0, y: 1.0 }; + let b = Point { x: 0.0, y: 0.0 }; + assert!(approx_eq(a.distance(&b), f64::sqrt(2.0))); +} + +#[test] +fn test_area() { + let circle = Circle::new(500.0, 400.0, 150.0); + assert!(approx_eq(circle.area(), 70685.83470577035)); +} + +#[test] +fn test_intersection() { + let circle = Circle::new(500.0, 500.0, 150.0); + let circle1 = Circle::new(80.0, 115.0, 30.0); + assert!(!circle.intersect(&circle1)); + let circle = Circle::new(100.0, 300.0, 150.0); + let circle1 = Circle::new(80.0, 115.0, 100.0); + assert!(circle.intersect(&circle1)); +} diff --git a/subjects/circle/README.md b/subjects/circle/README.md new file mode 100644 index 00000000..df8247e1 --- /dev/null +++ b/subjects/circle/README.md @@ -0,0 +1,95 @@ +## circle + +### Instructions + +Create the structures `Circle` and `Point` and the methods necessary for the code in [usage](#usage) to compile and run giving the expected result. + +Methods: + - Point; + distance() + - Circle: + diameter -> returns the diameter of the circle + area -> returns the area of the circle + intersection -> returns true if the 2 circles intersect +Associated Functions + - Circle: + new -> receives three 64 bits floating point numbers in the following + order: x, y and radius (x and y are the coordinates of the center). + and returns a new circle + + +### Expected Functions and Structures + +This snippets are incomplete and it's part of the exercise to discover the how to complete them + +```rust +struct Circle { + center //.. + radius //.. +} + +struct Point { +// ... +} + +// Point +fn distance(p1, p2) -> _ { + +} + +// Circle +fn new(x: f64, y: f64, radius: f64) -> Circle { +} + +fn diameter(_) -> _ { +} + +fn area() -> _ { +} +``` + +### Usage + +Here is a program to test your function + +```rust +use std::f64::consts; +use circle::{Circle, Point}; + +fn main() { + let circle = Circle::new(500.0, 500.0, 150.0); + let circle1 = Circle { + center: Point { x: 80.0, y: 115.0 }, + radius: 30.0, + }; + let point_a = Point { x: 1.0, y: 1.0 }; + let point_b = Point { x: 0.0, y: 0.0 }; + println!("circle = {:?} area = {}", circle, circle.area()); + println!("circle = {:?} diameter = {}", circle, circle.diameter()); + println!("circle1 = {:?} diameter = {}", circle1, circle1.diameter()); + println!( + "circle and circle1 intersect = {}", + circle.intersect(&circle1) + ); + + println!( + "distance between {:?} and {:?} is {}", + point_a, + point_b, + point_a.distance(&point_b) + ); + +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +circle = Circle { center: Point { x: 500.0, y: 500.0 }, radius: 150.0 } area = 70685.83470577035 +circle = Circle { center: Point { x: 500.0, y: 500.0 }, radius: 150.0 } diameter = 300 +circle1 = Circle { center: Point { x: 80.0, y: 115.0 }, radius: 30.0 } diameter = 60 +circle and circle1 intersect = false +distance between Point { x: 1.0, y: 1.0 } and Point { x: 0.0, y: 0.0 } is 1.4142135623730951 +student@ubuntu:~/[[ROOT]]/test$ +```