|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "eb0659da-c482-4295-a39e-cfabcb9dd616", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "# test_literals.py\n", |
| 11 | + "\n", |
| 12 | + "import pytest\n", |
| 13 | + "\n", |
| 14 | + "# Test for number literals\n", |
| 15 | + "def test_number_literals():\n", |
| 16 | + " # Binary, decimal, octal, and hexadecimal literals\n", |
| 17 | + " a = 0b1010\n", |
| 18 | + " b = 100\n", |
| 19 | + " c = 0o310\n", |
| 20 | + " d = 0x12c\n", |
| 21 | + "\n", |
| 22 | + " # Floats and complex literals\n", |
| 23 | + " float_1 = 10.5\n", |
| 24 | + " float_2 = 1.5e2\n", |
| 25 | + " float_3 = 1.5e-3\n", |
| 26 | + " x = 3.14j\n", |
| 27 | + "\n", |
| 28 | + " # Check number literals\n", |
| 29 | + " assert a == 10, f\"Expected 10, but got {a}\"\n", |
| 30 | + " assert b == 100, f\"Expected 100, but got {b}\"\n", |
| 31 | + " assert c == 200, f\"Expected 200, but got {c}\"\n", |
| 32 | + " assert d == 300, f\"Expected 300, but got {d}\"\n", |
| 33 | + "\n", |
| 34 | + " # Check float literals\n", |
| 35 | + " assert float_1 == 10.5, f\"Expected 10.5, but got {float_1}\"\n", |
| 36 | + " assert float_2 == 150.0, f\"Expected 150.0, but got {float_2}\"\n", |
| 37 | + " assert float_3 == 0.0015, f\"Expected 0.0015, but got {float_3}\"\n", |
| 38 | + "\n", |
| 39 | + " # Check complex literal\n", |
| 40 | + " assert x == 3.14j, f\"Expected 3.14j, but got {x}\"\n", |
| 41 | + " assert x.imag == 3.14, f\"Expected 3.14 as the imaginary part, but got {x.imag}\"\n", |
| 42 | + " assert x.real == 0.0, f\"Expected 0.0 as the real part, but got {x.real}\"\n", |
| 43 | + "\n", |
| 44 | + "# Test for string literals\n", |
| 45 | + "def test_string_literals():\n", |
| 46 | + " # String literals\n", |
| 47 | + " string_1 = 'Hello, world!'\n", |
| 48 | + " string_2 = \"Python programming\"\n", |
| 49 | + " string_3 = '''This is a\n", |
| 50 | + " multi-line string.'''\n", |
| 51 | + " string_4 = \"\"\"This is another\n", |
| 52 | + " multi-line string.\"\"\"\n", |
| 53 | + " string_5 = \"This is a newline:\\nAnd this is a tab:\\tHello\"\n", |
| 54 | + " string_6 = r\"C:\\Users\\Name\\Folder\"\n", |
| 55 | + " name = \"Alice\"\n", |
| 56 | + " string_7 = f\"Hello, {name}!\"\n", |
| 57 | + " string_8 = u\"\\U0001F604\"\n", |
| 58 | + " string_9 = b\"Hello, bytes!\"\n", |
| 59 | + "\n", |
| 60 | + " # Check string literals\n", |
| 61 | + " assert string_1 == 'Hello, world!', f\"Expected 'Hello, world!', but got {string_1}\"\n", |
| 62 | + " assert string_2 == \"Python programming\", f\"Expected 'Python programming', but got {string_2}\"\n", |
| 63 | + " assert string_3 == '''This is a\n", |
| 64 | + " multi-line string.''' , f\"Expected multi-line string, but got {string_3}\"\n", |
| 65 | + " assert string_4 == \"\"\"This is another\n", |
| 66 | + " multi-line string.\"\"\" , f\"Expected another multi-line string, but got {string_4}\"\n", |
| 67 | + " assert string_5 == \"This is a newline:\\nAnd this is a tab:\\tHello\", f\"Expected string with escape sequences, but got {string_5}\"\n", |
| 68 | + " assert string_6 == r\"C:\\Users\\Name\\Folder\", f\"Expected raw string, but got {string_6}\"\n", |
| 69 | + " assert string_7 == \"Hello, Alice!\", f\"Expected f-string 'Hello, Alice!', but got {string_7}\"\n", |
| 70 | + " assert string_8 == '😄', f\"Expected Unicode string '😄', but got {string_8}\"\n", |
| 71 | + " assert string_9 == b\"Hello, bytes!\", f\"Expected byte string b'Hello, bytes!', but got {string_9}\"\n", |
| 72 | + "\n", |
| 73 | + " # Test byte string iteration (checking byte values)\n", |
| 74 | + " expected_bytes = [72, 101, 108, 108, 111, 44, 32, 98, 121, 116, 101, 115, 33]\n", |
| 75 | + " byte_values = [byte for byte in string_9]\n", |
| 76 | + " assert byte_values == expected_bytes, f\"Expected byte values {expected_bytes}, but got {byte_values}\"\n", |
| 77 | + "\n", |
| 78 | + "# Test for boolean literals\n", |
| 79 | + "def test_boolean_literals():\n", |
| 80 | + " a = True + 4\n", |
| 81 | + " b = False + 10\n", |
| 82 | + "\n", |
| 83 | + " assert a == 5, f\"Expected 5, but got {a}\"\n", |
| 84 | + " assert b == 10, f\"Expected 10, but got {b}\"\n", |
| 85 | + "\n", |
| 86 | + "# Test for special literal\n", |
| 87 | + "def test_special_literal():\n", |
| 88 | + " a = None\n", |
| 89 | + " assert a is None, f\"Expected None, but got {a}\"\n", |
| 90 | + "\n" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": null, |
| 96 | + "id": "06d755be-d194-401d-9bc5-da6ce999c31e", |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [], |
| 99 | + "source": [] |
| 100 | + } |
| 101 | + ], |
| 102 | + "metadata": { |
| 103 | + "kernelspec": { |
| 104 | + "display_name": "Python 3 (ipykernel)", |
| 105 | + "language": "python", |
| 106 | + "name": "python3" |
| 107 | + }, |
| 108 | + "language_info": { |
| 109 | + "codemirror_mode": { |
| 110 | + "name": "ipython", |
| 111 | + "version": 3 |
| 112 | + }, |
| 113 | + "file_extension": ".py", |
| 114 | + "mimetype": "text/x-python", |
| 115 | + "name": "python", |
| 116 | + "nbconvert_exporter": "python", |
| 117 | + "pygments_lexer": "ipython3", |
| 118 | + "version": "3.13.1" |
| 119 | + } |
| 120 | + }, |
| 121 | + "nbformat": 4, |
| 122 | + "nbformat_minor": 5 |
| 123 | +} |
0 commit comments