Skip to content

Adding M.Q.Convert.DoubleAsStringWithPrecision function - #1664

Merged
Stefan J. Wernli (swernli) merged 22 commits into
microsoft:mainfrom
JPark1023:double-as-string
Jul 24, 2024
Merged

Stefan J. Wernli (swernli) merged 22 commits into
microsoft:mainfrom
JPark1023:double-as-string

Conversation

@JPark1023

Copy link
Copy Markdown
Contributor

Resolves #1406

Comment thread library/std/convert.qs Resolved
Comment thread library/std/convert.qs Resolved
@tcNickolas

Copy link
Copy Markdown
Contributor

We need to include a unit test for the new function. I think a similar test is in compiler\qsc_eval\src\intrinsic\tests.rs, for example, int_as_double() test

@JPark1023 JPark1023 changed the title Adding M.Q.Convert.DoubleAsStringWithFormat function Adding M.Q.Convert.DoubleAsStringWithPrecision function Jun 27, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good start, thanks for working on this! I left a few comments, and we'll need to make sure this new function is covered by Rust unit tests as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there are still some unit test failures due to a span change. Otherwise the code is looking good!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at the API docs side of things and left some comments. Once you resolve conflicts and make sure all tests pass, we'll take another look, and this should be good to go.

Thank you!

Comment thread library/std/convert.qs Resolved
Comment thread library/std/convert.qs Resolved
Comment thread library/std/convert.qs Resolved
@tcNickolas

Copy link
Copy Markdown
Contributor

Looks like there are Q# formatting issues and failing tests, though I'm not sure why the unrelated tests fail. Stefan J. Wernli (@swernli) Any ideas?

@swernli

Copy link
Copy Markdown
Contributor

Looks like there are Q# formatting issues and failing tests, though I'm not sure why the unrelated tests fail. Stefan J. Wernli (@swernli) Any ideas?

They are related... the formatting or addition/removal of comments can change the contents of the file and makes the spans different that the test relies on. In addition the Q# file formatting, you can run env UPDATE_EXPECT=1 cargo test which will update the expect tests. Take note that sometimes if the input Q# string in the unit test has trailing spaces on any lines, the Rust formatting often removes those and can cause variations in the spans, so make sure those nested strings in the unit tests don't have any trailing spaces.

@tcNickolas

Copy link
Copy Markdown
Contributor

They are related... the formatting or addition/removal of comments can change the contents of the file and makes the spans different that the test relies on. In addition the Q# file formatting, you can run env UPDATE_EXPECT=1 cargo test which will update the expect tests. Take note that sometimes if the input Q# string in the unit test has trailing spaces on any lines, the Rust formatting often removes those and can cause variations in the spans, so make sure those nested strings in the unit tests don't have any trailing spaces.

I don't think this is documented in the repo - this is only of the only two mentions of UPDATE_EXPECT, and the other one is in a PR as well. Could you please document this for the contributors unfamiliar with how our tests work (including me :-) )?

@swernli

Copy link
Copy Markdown
Contributor

I don't think this is documented in the repo - this is only of the only two mentions of UPDATE_EXPECT, and the other one is in a PR as well. Could you please document this for the contributors unfamiliar with how our tests work (including me :-) )?

I don't think it makes sense to document this in the repo since it's in the error message from the test failure (see https://github.com/microsoft/qsharp/actions/runs/9899922588/job/27378539797?pr=1664#step:9:1218) and shows up locally when running cargo test or python build.py as well. It's a feature of the Rust test library we are using (expect_test), so any time an expect test fails it tells users in the test output how to automatically update:

  You can update all `expect!` tests by running:
  
      env UPDATE_EXPECT=1 cargo test
  
  To update a single test, place the cursor on `expect` token and use `run` feature of rust-analyzer.

@JPark1023

Copy link
Copy Markdown
Contributor Author

Where do I run env UPDATE_EXPECT=1 cargo test ? It doesn't recognise 'env' when running it in the root repo

@swernli

Copy link
Copy Markdown
Contributor

Where do I run env UPDATE_EXPECT=1 cargo test ? It doesn't recognise 'env' when running it in the root repo

env is the Linux command line mechanism for setting environment variables. If you are Windows, you'll want to use set UPDATE_EXPECT=1 followed by cargo test instead. Note that on Windows environment variables remain set for the duration of that terminal instance when set this way.

@JPark1023

Copy link
Copy Markdown
Contributor Author

Couldn't find any trailing spaces in the unit tests and formatted the Q# files but those 2 tests are still failings. I'm not sure where to go from here, any suggestions?

@tcNickolas

Copy link
Copy Markdown
Contributor

Can you push your latest changes to this pull request, so that we are looking at the same code as you are?

@tcNickolas

Copy link
Copy Markdown
Contributor

When I tried running cargo run --release --bin qsc_formatter -- ./library/ ./samples/ -r --write, the change it did was to rearrange the lines in the export statement back into a single line.

@tcNickolas

Copy link
Copy Markdown
Contributor

Stefan J. Wernli (@swernli) Can you help us understand the expected behavior of the env UPDATE_EXPECT=1 cargo test command?

When I run it (on Windows, so splitting it in two, set UPDATE_EXPECT=1 then cargo test), it shows me the failing test and the diff between expected and actual, but it doesn't update the compiler\qsc_eval\src\tests.rs file itself. If I change LocalItemId from 123 to 124 by hand in the two failing tests, this fixes them, but that's not quite what I expected from "automatically update".

Is this the expected behavior, or did I set something up wrong?

@swernli

Copy link
Copy Markdown
Contributor

Since I'm on macOS, env UPDATE_EXPECT=1 cargo test works as expected from the cmdline. On Windows you can also try using PowerShell, which makes persisting environment variables easier (see about Environment Variables). You can also use several alternatives built-into VS Code via the rust-analyzer extension. With your cursor in one of the expect! macros, you can use the command palette to bring up the rust-analyzer: Run command:
image

It will bring up the same options for running tests that it usually does (running the individual test, running the tests in the module) but with the additional "expect" options automatically set:
image

Selecting one of those will run the test with the option set to regenerate the content to match what is returned. So you can do this on a per-function or per-module. There are some short video walkthroughs of this feature are on the expect-test repo: https://github.com/rust-analyzer/expect-test

JPark1023, could you try one of these methods and see if it works for you?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thank you for working on this!

@swernli
Stefan J. Wernli (swernli) added this pull request to the merge queue Jul 24, 2024
Merged via the queue into microsoft:main with commit 1122c4a Jul 24, 2024
@JPark1023

Copy link
Copy Markdown
Contributor Author

When I tried running cargo run --release --bin qsc_formatter -- ./library/ ./samples/ -r --write, the change it did was to rearrange the lines in the export statement back into a single line.

Running Format Document with Azure Quantum Development Kit rearranged it with each function being on a separate line. What's the difference between those two ways of formatting?

@tcNickolas

Copy link
Copy Markdown
Contributor

I suspect that "Format Document" comes from the installed extension, the latest released version, and "cargo run" comes from the code in the branch, so the latest code in main. If changes were done to that logic since the last release, the results will change. I haven't tracked that down to confirm, though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add M.Q.Convert.DoubleAsStringWithFormat

4 participants