[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip / qa] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


I really love that I can put some small unit tests right into the impl file in Rust. boomer languages require you to create an entire project for tests.
>>
also OP here, there's a bug in this code
figure it out if you can :^)
>>
>>103293496
asserting floats to be equal is retarded. different inequalities in the part costs. not doing the math
>>
All of these Rust threads from nowhere happening all at the same time are totally organic.
>>
>>103293419
There are reasons why tests in the same project is a very bad idea. You'll learn once you do software for a living instead of larping at home.
>>
>>103293616
For small unit tests, it's totally fine.
>>
>>103293616
And why is that? Can you elaborate in your own words?
>>
File: 1721511391504225.jpg (111 KB, 975x850)
111 KB
111 KB JPG
>>103293496
>Float money
>>
>>103293756
float is f32
>>
>>103293419
why do you have to specify that ugly `cfg` test thing.
and then mark every function with `test` as well.
That reeks of cnile / c++ behavior
>>
>>103293923
>why do you have to specify that ugly `cfg` test thing.
so that the compiler can tell which code is only parsed and executed when running the test subcommand

>and then mark every function with `test` as well.
so that the compiler can tell which functions to automatically run as part of the test module, and which functions are only helper methods for tests in the same module
>>
>>103293946
seems cringe
in D it's merely
unittest
{
...
}

then to run
dub test

Why does Rust make things so tedious?
>>
>>103293826
>float[ing point] money
>>
>>103293616
D also supports this with the unittest {} block
C++ can do it using doctest, tests get stripped when you do a release build or specify a single macro that says don't embed. It's really not a big deal.

t. works on software for a living where they do this with no ill effects.
>>
>>103293419
>uses float in money related calculations
why are rustrannies this stupid holy shit
>>
>>103293496
nobody fucking cares, faggot.

>>103293616
these same flea eating primates can't do anything else but stackoverflow exercises.

>>103293946
> take thing that already exists in other languages
> make it more convoluted
really is just pointless busywork for those that feast on bugs found in their wife's boyfriend's fur, isn't it?
>>
>>103293419
>not testing inputs
Useless, but yeah that feature is cool.
>>
>>103294472
contract programming isn't fashionable yet.
Maybe Rust2 will change that when it reinvents it with a gimmicky name
>>
>>103294472
>>103294516
inputs should be sanitized before sending into functions.
>>
>>103294530
no, contract programming and type-classes (concepts for C++niles) already solve this.
>>
>>103294516
they're working on contracts and verification
>>
>>103294516
> can't even get rust v1 to a point where feature creep, bug fixes and standards get frozen
> now they're hoping for rust v2
lmao. the absolute state of this board
>>
>>103293756
>not using floats on your eshop and shaving off the floating point error to your advantage
>>
>>103293756
Why not? It's SAFE!!!!!!!!!!!
>>
>>103294636
>noooooooo you can't just keep adding features to a language and progressing it, you have to make it stagnant like C/C++ and take 5 years to even add a simple feature that will take another 5 years to be implemented in any compiler
>>
>>103293616
Whatever your position on other things, on tests you're an ignorant troglodyte. Reality is far more complicated than you think.
This industry makes it far too easy to get promoted. You should still be a junior.
>>
>>103293616
If your code base can't pass simple unit tests within it then it has no business heading off to proper QA testing and wasting everyone's time
>>
>>103294530
where do you sanitize the inputs then? do you send hit squads to kill any users who are so foolish as to make a typo?
>>
>>103293602
Viruses are organic. Even mindviruses like Rust.
>>
>>103294916
My codebase passes tests, but i'm not a retard who shoves them in the same project :)

>>103294895
what are you going on about? i never said tests are bad, i said test in the same project are bad.
>>
>>103294890
this is why nobody takes rust seriously. you can't easily support something that will change at a later update because a fat tranny fat fingered an entire macro or because what they thought was "safe" led to security vulnerabilities that are in every way C-like as you can imagine.
>>
>>103294916
anon you're replying to is right. i've never seen busy work tests put into code like this ever unless it's to test the compiler (usually far more complicated than the toddler level homework used in op's example and usually separate from any real code) but i have never seen this cancer in production code before. it wouldn't even pass my qa. it would be sent back to the retarded toddler that vomited it up and he'd be forced to remove it
>>
>>103293616
>There are reasons why tests in the same project is a very bad idea.
Because you use a shitlang
>>
>>103294890
>you have to make it stagnant like C/C++
have you seen the abomination that C++ is exactly because they keep bolting badly designed features onto it
>>
>>103295000
>i said test in the same project are bad
Either you're using that retarded MS definition of "project" or you're full of shit.
>>
>>103293419
Probably the thing with the giant squiggles underneath, indicating some form of warning.
>>
>>103295330
have you tried not using them and only the things that are useful?
>>
>>103293496
>not testing with boundary values
pure junior coder shit
>>
>>103293419
as i32 as f64;

What an absolute joke of a language
>>
>>103295499
What's wrong with it?
>>
>>103295499
it is probably for rounding the number
>>
>>103293419
You can also shit in the streets Rajeev but the rest of us would rather use a toilet.
>>
>>103295499
>ive never casted an int to a float
hate the meme lang for valid reasons.
don't retard yourself.
>>
>>103295499
Actual projects that people rely on and that actually make money need that level of precision kid...
>>
File: 1731316666010444.png (175 KB, 392x536)
175 KB
175 KB PNG
fn calculate_quote(part_cost: f64) -> f64 {
let over_200 = (part_cost >= 200.) as i32 as f64;
let over_350 = (part_cost > 350.) as i32 as f64;

part_cost + 140. + 40. * over_200 + 70. * over_350
}

#[cfg(test)]
mod tests {
fn calculate_quote(part_cost: f64) -> f64 {
if part_cost < 200. {
part_cost + 140.
} else if part_cost <= 350. {
part_cost + 180.
} else {
part_cost + 250.
}
}

#[test]
fn tset() {
for i in 0 .. 1000 {
let i = i as f64;
assert_eq!(self::calculate_quote(i), super::calculate_quote(i));
}
}
}
>>
>>103294868
Rust is designed for MEMORY safety. Choosing a float over another numeric data type is perfectly memory safe. You will not corrupt any memory. But it is still a logic error. IEEE 754 does not accurately represent base 10 fractions and therefore should not be used for decimal currency. If you are using Rust for money, you have several options. You can store the lowest possible denomination of currency as an integer. You can also use a decimal type library, such as rust_decimal:
https://github.com/paupino/rust-decimal
(yes this is the real correct rust_decimal crate. The typosquatting malware crate rustdecimal has been deleted)

>>103294021
This isn't tedium, it's a pretty common approach to language design. You ever notice in Python and Java how some types and methods might get annotated with @something(like_this)? Rust similarly allows annotations, and they #[look(like_this)]. It allows you to avoid a keyword so you can make other ways to do conditional compilation in the future. This syntax is also used for proc macros, so you can tell a macro to do something with that function during compilation. That's a low of power for language design at compile time without needing to make new keywords for every new thing you want to do.
>>
>>103295555
What software works with numbers. Numbers are so fucking gay bruh. Real software just does syscalls n shit.
>>
>>103293419
Disgusting, there is not room for unit testing in the implementation that is niggerlicious. Pls recapacite.
>>
>>103296443
emotional ESL nonsense
>>
>>103293419
You need 3 fucking tests to perform basic arithmetic? You are pathetic.
>>
You are literally retarded my guy. No one above an IQ of 110 would need to create 3 tests to check arithmetic, because anyone above that IQ level can just do it in their head. You're just writing a bunch of trivial tests to pad out for scripts to make it look like you're coding, but you're not, and you will never be a real programmer.
>>
>>103293602
Exactly absolutely a glowie lang they’re not even hiding it anymore.



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.