TestAPI.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGL/GL/gl.h>
  7. #include <LibGL/GLContext.h>
  8. #include <LibTest/TestCase.h>
  9. static NonnullOwnPtr<GL::GLContext> create_testing_context()
  10. {
  11. auto bitmap = MUST(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { 1, 1 }));
  12. auto context = MUST(GL::create_context(*bitmap));
  13. GL::make_context_current(context);
  14. return context;
  15. }
  16. TEST_CASE(0001_gl_gen_textures_does_not_return_the_same_texture_name_twice_unless_deleted)
  17. {
  18. // https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGenTextures.xhtml
  19. // "Texture names returned by a call to glGenTextures are not returned by subsequent calls, unless they are first deleted with glDeleteTextures."
  20. auto context = create_testing_context();
  21. GLuint texture1 = 0;
  22. GLuint texture2 = 0;
  23. glGenTextures(1, &texture1);
  24. // glDeleteTextures previously did not check that the texture name was allocated by glGenTextures before adding it to the free texture name list.
  25. // This means that if you delete a texture twice in a row, the name will appear twice in the free texture list, making glGenTextures return the
  26. // same texture name twice in a row.
  27. glDeleteTextures(1, &texture1);
  28. glDeleteTextures(1, &texture1);
  29. texture1 = 0;
  30. glGenTextures(1, &texture1);
  31. glGenTextures(1, &texture2);
  32. EXPECT_NE(texture1, texture2);
  33. }
  34. TEST_CASE(0002_gl_cull_face_does_not_accept_left_and_right)
  35. {
  36. auto context = create_testing_context();
  37. // glCullFace only accepts GL_FRONT, GL_BACK and GL_FRONT_AND_BACK. We checked if the mode was valid by performing cull_mode < GL_FRONT || cull_mode > GL_FRONT_AND_BACK.
  38. // However, this range also contains GL_LEFT and GL_RIGHT, which we would accept when we should return a GL_INVALID_ENUM error.
  39. glCullFace(GL_LEFT);
  40. EXPECT_EQ(glGetError(), static_cast<GLenum>(GL_INVALID_ENUM));
  41. glCullFace(GL_RIGHT);
  42. EXPECT_EQ(glGetError(), static_cast<GLenum>(GL_INVALID_ENUM));
  43. }