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

⚡️ Speed up method Font._registered_font_key by 20% in core/src/toga/fonts.py #1

Open
wants to merge 1 commit into
base: codeflash-run
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Oct 29, 2024

📄 Font._registered_font_key() in core/src/toga/fonts.py

📈 Performance improved by 20% (0.20x faster)

⏱️ Runtime went down from 227 microseconds to 190 microseconds (best of 193 runs)

Explanation and details

Explanation of Optimizations.

Correctness verification

The new optimized code was tested for correctness. The results are listed below.

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 1043 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# imports
# function to test
from __future__ import annotations

import pytest  # used for our unit tests
from toga.fonts import Font
from toga.platform import get_platform_factory
from travertino import constants
from travertino.constants import NORMAL

# unit tests

def test_valid_inputs():
    # Test with all valid parameters
    codeflash_output = Font._registered_font_key("Arial", "bold", "italic", "small-caps")
    codeflash_output = Font._registered_font_key("Times New Roman", "light", "normal", "normal")
    # Outputs were verified to be equal to the original implementation

def test_invalid_weight():
    # Test with invalid weight
    codeflash_output = Font._registered_font_key("Arial", "extra-bold", "italic", "small-caps")
    codeflash_output = Font._registered_font_key("Verdana", "super-light", "normal", "normal")
    # Outputs were verified to be equal to the original implementation

def test_invalid_style():
    # Test with invalid style
    codeflash_output = Font._registered_font_key("Arial", "bold", "slanted", "small-caps")
    codeflash_output = Font._registered_font_key("Verdana", "light", "oblique", "normal")
    # Outputs were verified to be equal to the original implementation

def test_invalid_variant():
    # Test with invalid variant
    codeflash_output = Font._registered_font_key("Arial", "bold", "italic", "extra-small-caps")
    codeflash_output = Font._registered_font_key("Verdana", "light", "normal", "large-caps")
    # Outputs were verified to be equal to the original implementation

def test_combination_invalid_parameters():
    # Test with multiple invalid parameters
    codeflash_output = Font._registered_font_key("Arial", "extra-bold", "slanted", "extra-small-caps")
    codeflash_output = Font._registered_font_key("Verdana", "super-light", "oblique", "large-caps")
    # Outputs were verified to be equal to the original implementation

def test_edge_cases():
    # Test with empty strings
    codeflash_output = Font._registered_font_key("", "", "", "")
    
    # Test with very long strings
    long_family = "A" * 1000
    long_variant = "small-caps" + "A" * 1000
    codeflash_output = Font._registered_font_key(long_family, "bold", "italic", "small-caps")
    codeflash_output = Font._registered_font_key("Arial", "bold", "italic", long_variant)
    # Outputs were verified to be equal to the original implementation

def test_case_sensitivity():
    # Test with different cases for valid parameters
    codeflash_output = Font._registered_font_key("Arial", "BOLD", "ITALIC", "SMALL-CAPS")
    codeflash_output = Font._registered_font_key("Verdana", "Light", "Normal", "Normal")
    # Outputs were verified to be equal to the original implementation

def test_non_string_inputs():
    # Test with non-string types
    codeflash_output = Font._registered_font_key(123, "bold", "italic", "small-caps")
    codeflash_output = Font._registered_font_key("Arial", None, "italic", "small-caps")
    codeflash_output = Font._registered_font_key("Arial", "bold", [], "small-caps")
    codeflash_output = Font._registered_font_key("Arial", "bold", "italic", {})
    # Outputs were verified to be equal to the original implementation

def test_large_scale():
    # Generate a large dataset with various combinations
    families = ["Arial", "Verdana", "Times New Roman"]
    weights = ["bold", "light", "extra-bold", "normal"]
    styles = ["italic", "normal", "slanted"]
    variants = ["small-caps", "normal", "large-caps"]
    
    for family in families:
        for weight in weights:
            for style in styles:
                for variant in variants:
                    Font._registered_font_key(family, weight, style, variant)
    # Outputs were verified to be equal to the original implementation

def test_boundary_values():
    # Test with boundary values
    codeflash_output = Font._registered_font_key("Arial", "bold", "italic", "small-caps")
    long_weight = "bold" + "A" * 1000
    codeflash_output = Font._registered_font_key("Arial", long_weight, "italic", "small-caps")
    # Outputs were verified to be equal to the original implementation
# imports
# function to test
from __future__ import annotations

import pytest  # used for our unit tests
from toga.fonts import Font
from toga.platform import get_platform_factory
from travertino import constants
from travertino.constants import NORMAL

# unit tests

def test_valid_inputs():
    """Test with all valid inputs."""
    codeflash_output = Font._registered_font_key("Arial", "bold", "italic", "small-caps")
    codeflash_output = Font._registered_font_key("Times New Roman", "normal", "normal", "normal")
    # Outputs were verified to be equal to the original implementation

def test_invalid_weight():
    """Test with invalid weight."""
    codeflash_output = Font._registered_font_key("Arial", "extra-bold", "italic", "small-caps")
    codeflash_output = Font._registered_font_key("Times New Roman", "lightest", "normal", "normal")
    # Outputs were verified to be equal to the original implementation

def test_invalid_style():
    """Test with invalid style."""
    codeflash_output = Font._registered_font_key("Arial", "bold", "slanted", "small-caps")
    codeflash_output = Font._registered_font_key("Times New Roman", "normal", "oblique", "normal")
    # Outputs were verified to be equal to the original implementation

def test_invalid_variant():
    """Test with invalid variant."""
    codeflash_output = Font._registered_font_key("Arial", "bold", "italic", "large-caps")
    codeflash_output = Font._registered_font_key("Times New Roman", "normal", "normal", "tiny-caps")
    # Outputs were verified to be equal to the original implementation

def test_multiple_invalid_inputs():
    """Test with multiple invalid inputs."""
    codeflash_output = Font._registered_font_key("Arial", "extra-bold", "slanted", "large-caps")
    codeflash_output = Font._registered_font_key("Times New Roman", "lightest", "oblique", "tiny-caps")
    # Outputs were verified to be equal to the original implementation

def test_empty_strings():
    """Test with empty string inputs."""
    codeflash_output = Font._registered_font_key("", "", "", "")
    # Outputs were verified to be equal to the original implementation

def test_mixed_valid_and_invalid():
    """Test with mixed valid and invalid inputs."""
    codeflash_output = Font._registered_font_key("Arial", "bold", "italic", "large-caps")
    codeflash_output = Font._registered_font_key("Times New Roman", "lightest", "normal", "small-caps")
    # Outputs were verified to be equal to the original implementation

def test_case_sensitivity():
    """Test with different cases."""
    codeflash_output = Font._registered_font_key("arial", "BOLD", "Italic", "SMALL-CAPS")
    codeflash_output = Font._registered_font_key("TIMES NEW ROMAN", "Normal", "NORMAL", "normal")
    # Outputs were verified to be equal to the original implementation

def test_special_characters():
    """Test with special characters."""
    codeflash_output = Font._registered_font_key("Arial!", "bold", "italic", "small-caps")
    codeflash_output = Font._registered_font_key("Times New Roman", "bold", "italic ", "small-caps")
    # Outputs were verified to be equal to the original implementation

def test_large_scale():
    """Test with a large number of font attributes."""
    for i in range(1000):
        family = f"FontFamily{i}"
        weight = "bold" if i % 2 == 0 else "extra-bold"
        style = "italic" if i % 3 == 0 else "slanted"
        variant = "small-caps" if i % 5 == 0 else "large-caps"
        expected_weight = "bold" if i % 2 == 0 else "normal"
        expected_style = "italic" if i % 3 == 0 else "normal"
        expected_variant = "small-caps" if i % 5 == 0 else "normal"
        codeflash_output = Font._registered_font_key(family, weight, style, variant)
    # Outputs were verified to be equal to the original implementation

def test_boundary_values():
    """Test with boundary values."""
    codeflash_output = Font._registered_font_key("Arial", "bold", "italic", "small-caps")
    codeflash_output = Font._registered_font_key("Arial", "bold", "italic", "small-caps ")
    # Outputs were verified to be equal to the original implementation

def test_non_string_inputs():
    """Test with non-string inputs."""
    codeflash_output = Font._registered_font_key(123, "bold", "italic", "small-caps")
    codeflash_output = Font._registered_font_key("Arial", None, "italic", "small-caps")
    # Outputs were verified to be equal to the original implementation

def test_default_values():
    """Test with default values."""
    codeflash_output = Font._registered_font_key("Arial", "normal", "normal", "normal")
    codeflash_output = Font._registered_font_key("Times New Roman", "normal", "normal", "normal")
    # Outputs were verified to be equal to the original implementation

🔘 (none found) − ⏪ Replay Tests

@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 29, 2024
@codeflash-ai codeflash-ai bot requested a review from KRRT7 October 29, 2024 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡️ codeflash Optimization PR opened by Codeflash AI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants