Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker, cgen: implement methods reverse()/reverse_in_place() for fixed array #22712

Merged
merged 1 commit into from
Oct 31, 2024

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Oct 31, 2024

This PR implement methods reverse()/reverse_in_place() for fixed array.

  • Implement methods reverse()/reverse_in_place() for fixed array.
  • Add tests.
struct User {
	age  int
	name string
}

fn test_fixed_array_reverse_in_place() {
	mut a := ['hi', '1', '5', '3']!
	a.reverse_in_place()
	assert a == ['3', '5', '1', 'hi']!

	mut nums := [67, -3, 108, 42, 7]!
	nums.reverse_in_place()
	assert nums == [7, 42, 108, -3, 67]!

	mut users := [User{22, 'Peter'}, User{20, 'Bob'}, User{25, 'Alice'}]!
	users.reverse_in_place()
	assert users[0].age == 25
	assert users[1].age == 20
	assert users[2].age == 22
	assert users[0].name == 'Alice'
	assert users[1].name == 'Bob'
	assert users[2].name == 'Peter'
}

fn test_fixed_array_reverse() {
	a := ['hi', '1', '5', '3']!
	b := a.reverse()
	assert a == ['hi', '1', '5', '3']!
	assert b == ['3', '5', '1', 'hi']!

	mut nums := [67, -3, 108, 42, 7]!
	n := nums.reverse()
	assert nums == [67, -3, 108, 42, 7]!
	assert n == [7, 42, 108, -3, 67]!

	mut users := [User{22, 'Peter'}, User{20, 'Bob'}, User{25, 'Alice'}]!
	u := users.reverse()

	assert users[0].age == 22
	assert users[1].age == 20
	assert users[2].age == 25
	assert users[0].name == 'Peter'
	assert users[1].name == 'Bob'
	assert users[2].name == 'Alice'

	assert u[0].age == 25
	assert u[1].age == 20
	assert u[2].age == 22
	assert u[0].name == 'Alice'
	assert u[1].name == 'Bob'
	assert u[2].name == 'Peter'
}

Huly®: V_0.6-21160

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

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

Excellent work.

@spytheman spytheman merged commit 97941b9 into vlang:master Oct 31, 2024
78 checks passed
@yuyi98 yuyi98 deleted the impl_fixed_array_reverse branch October 31, 2024 12:03
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.

2 participants