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

Potentially inconsistent List behavior for insert_after and insert_before for when an element doesn't exist #42116

Open
Xrayez opened this issue Sep 16, 2020 · 0 comments

Comments

@Xrayez
Copy link
Contributor

Xrayez commented Sep 16, 2020

Godot version:
6f4384f

Issue description:
I know this may be silly and not sure whether this could be a copy-paste mistake or indented, and I'm not that experienced with using std::list either to know the expected behavior.

If the element doesn't exist in the list (nullptr), then insert_after constructs a new element and pushes it back to the list

godot/core/list.h

Lines 279 to 284 in 6f4384f

Element *insert_after(Element *p_element, const T &p_value) {
CRASH_COND(p_element && (!_data || p_element->data != _data));
if (!p_element) {
return push_back(p_value);
}

But then if we take the same situation but use insert_before, it does the same:

godot/core/list.h

Lines 305 to 310 in 6f4384f

Element *insert_before(Element *p_element, const T &p_value) {
CRASH_COND(p_element && (!_data || p_element->data != _data));
if (!p_element) {
return push_back(p_value);
}

Expected result:
The insert_before should push_front() if the target element doesn't exist in the list, and not push_back.

The other question is that whether this behavior should be allowed in the first place, and simply error-out on attempting to insert a new element before or after an element which doesn't exists.

Steps to reproduce:
Some GUT pseudo-code:

func test_insert_after_null():
	populate_test_data(list)
	list.insert_after(null, "Godot")
	assert_eq(list.back.value, "Godot") # OK.


func test_insert_before_null():
	populate_test_data(list)
	list.insert_before(null, "Godot")
	assert_eq(list.front.value, "Godot") # Fails here.

Minimal reproduction project:
Not minimal but the context this was found: goostengine/goost#12.

@Xrayez Xrayez changed the title Potentially inconsistent List behavior for insert_after and insert_before for when an element doesn't exists Potentially inconsistent List behavior for insert_after and insert_before for when an element doesn't exist Sep 16, 2020
@akien-mga akien-mga added this to the 4.0 milestone Sep 23, 2020
@YuriSizov YuriSizov added discussion and removed bug labels Feb 23, 2023
@YuriSizov YuriSizov removed this from the 4.0 milestone Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants